简体   繁体   中英

setTimeout behavior with jquery and ajax

It is my understanding that setTimeout() can be used to stop code from executing temporarily, however this doesn't seem to be the case in the following code:

...}).done(function recursionLoad(){     
            var timerLoad = setTimeout(function(){


                },3000)

                $.ajax({
           type:'GET',
           url:'modelBN.xml',
           beforeSend: function(){$('#query-results').html('<img src="images/ajax-loader.gif"><p>Loading...</p>'); },
           timeout: 10000,
           error: function(xhr, status, error){...

So what happens is the AJAX call get made immediately instead being delayed for 3 seconds. Am I just using setTimeout incorrectly or is there something about AJAX that prevents it from working? Thanks for any assistance

setTimeout will call the function you pass to it (in the first argument) after the time you specify in the second argument.

It is not a sleep function, and it won't block other code from running.

If you want to run your call to $.ajax after the time has elapsed, then you need to do so from the function you pass to setTimeout (as opposed to calling setTimeout (with a function that will do nothing after 3 seconds) and then immediately calling $.ajax ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM