0

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

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Great. Thanks. My only problem is that I may have to use setTimeout more than once. So I am waiting for a file and just checking for it periodically. If I don't find the file I call setTimeout again from within the ajax call.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.