#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0

    How to wait until AJAX request is finished


    Is it possible to wait until an AJAX request is completed before moving on to the next instruction. If an example is needed I will be happy to supply it.

    BTW, I'm using jQuery for my AJAX requests

    Thanks
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,749
    Rep Power
    9397
    Yeah. Do the send() synchronously - false for the third argument.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    I'm sorry, I'm I forgot to mention that I am using jQuery for my AJAX request. Any ideas how to do it while useing jQuery.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Montana
    Posts
    504
    Rep Power
    13
    Base you next task on the response you get back.

    Code:
    $.get('URL', function(data) {
    		if(data = "") {
    			do this		 
    		}
    		else {
    			do that		
    		}
    });
    Have Eternal Life
    Learning is so unproductive...
    The more I learn the more questions I have!
    Therefore I am going backwards.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    That wont work in my situation, I have a function being called and inside that function is were the AJAX request are sent. What I need it to do is to wait until the function is completed before moving on.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2001
    Location
    Montana
    Posts
    504
    Rep Power
    13
    It still does not seem to be thought through properly. Why do you need to wait for Ajax to finish? It must be because there is some results you want to happen before moving on. Right? The way you know that the result you desire has happen is with the callback function, this tells you the Ajax request was either successful or failed.
    Have Eternal Life
    Learning is so unproductive...
    The more I learn the more questions I have!
    Therefore I am going backwards.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    This is true, and thanks again for your responses. The only problem is that every time I call the function, it does something different after it is completed. So if I do it your way I would have to make a new function for each call that does what I would like afterward, this would then remove any reason of having a function at all.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    383
    Rep Power
    47
    try this:

    Code:
    $.ajax({
    
       complete: function(){
         // put you code in here
       }
       
     });

    not tried but that is what the spec says.


    http://docs.jquery.com/Ajax_Events
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    23
    Rep Power
    0
    Thank you everyone, but I have figured it out.

    An AJAX request written using jQuery looks like this

    javascript Code:
     
    $.ajax({
       type: "POST",
       url: "url.php",
       success: function(message){
          alert(message);
       }
    });


    But if you want your JavaScript to wait until the AJAX request is complete you would just do...
    javascript Code:
     
    $.ajax({
       type: "POST",
       url: "url.php",
       async: false,
       success: function(message){
          alert(message);
       }
    });

IMN logo majestic logo threadwatch logo seochat tools logo