execute javascript only after json callback success

Discussion in 'jQuery' started by shyama, Feb 1, 2012.

  1. #1
    Hi,

    I am calling a third party webservice using getJSON callback. The function donot have a success handler attached to the function and since it is third party, I cannot edit the code also. By the time I receive the response, some of the javascript function in the page already executes.

    Is there any way I can delay the execution of the javascript function till I receive the response data from web service except settimeout method.

    Thanks
    Shyama
     
    shyama, Feb 1, 2012 IP
  2. maureeeteeeee

    maureeeteeeee Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    2
    Trophy Points:
    41
    #2
    Are you using jQuery .ajax(), if so use "success:" in the .ajax() form and determine the function :)

    Or, you could set a trigger var to false, and when the JSON is processed, set it to true and run the other functions
     
    maureeeteeeee, Feb 1, 2012 IP
  3. shyama

    shyama Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!! but the issue is I don't have control over jQuery JSON callback function. So i cannot use "success:" in .ajax. My requirment is that no javascript function should execute till I receive the response from JSON callback. Is there anyway I can explicity call success function from my page?
     
    shyama, Feb 1, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    Only if you're using AJAX. Then it's inherent - the page you call sends back a status 200, which jQuery uses to run the success function. If the page you're calling doesn't throw a status 200, use another site - whoever butchered ("wrote" is too elegant for what they did) that site can't be trusted.

    If you're not using AJAX, put what you get into a variable that's blank or null until the JSON return. Loop on the variable being empty. (That could hang the page, though, if the data connection is lost.) Calling another site that doesn't signal that it's returning data is a bad idea.
     
    Rukbat, Feb 25, 2012 IP