1

Possible Duplicate:
Why does setTimeout(fn, 0) sometimes help?

Reading jQuery 1.8 source,

WHY does it do setTimeout with 0 ms delay ? (instead of just executing the callback ?)

https://github.com/jquery/jquery/blob/1.8.0/src/ajax/xhr.js#L196

                if ( !s.async ) {                       
                    callback();
                } else if ( xhr.readyState === 4 ) {


                    // (IE6 & IE7) if it's in cache and has been
                    // retrieved directly we need to fire the callback

         //-------->// WHY do setTimeout with 0 ms delay ?
                    setTimeout( callback, 0 );
                } else {
                    handle = ++xhrId;
Community
  • 1
  • 1
Lydon Ch
  • 8,637
  • 20
  • 79
  • 132
  • @Strelok not really a duplicate IMHO, although it is related. In this instance it's needed to maintain the functional contract with the programmer, and not to work around any particular issue. – Alnitak Aug 22 '12 at 13:10
  • ah, actually it's to work around a peculiarity of IE6/7 – Alnitak Aug 22 '12 at 13:21

2 Answers2

3

This is a workaround for a peculiarity of IE6 and IE7 which can retrieve an AJAX result from cache without firing the XMLHTTPRequest callback, immediately setting its readyState property to 4, instead.

However the API contract for $.ajax requires that it return immediately for an asynchronous request (i.e. without calling the programmer-supplied callbacks).

Hence $.ajax call tests for those cached results, and then fakes the required asynchronous callback using setTimeout.

The $.ajax call completes, and as soon as the browser re-enters its event processing loop it'll find the (immediately expired) timer event and call its callbacks.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • so without doing setTimeout, current (executing?) function will be stopped to execute callback()? when you said current function, does it mean the function that encloses the if statement (which is #send() ) – Lydon Ch Aug 22 '12 at 13:10
  • @portoalet I've rewritten this answer almost entirely to try to make it clearer. – Alnitak Aug 22 '12 at 13:25
  • makes more sense after reading john resig article http://ejohn.org/blog/how-javascript-timers-work/ – Lydon Ch Aug 22 '12 at 14:31
2

The reason is that setTimeout adds the function to the browser event queue so it will only be invoked after the preceding events in the queue have been handled, allowing the rest of the function that sets the timeout to finish execution.

Asciiom
  • 9,867
  • 7
  • 38
  • 57
  • 1
    what does "remove the function from the execution queue" mean? – Alnitak Aug 22 '12 at 13:07
  • I don't understand why this is getting up voted. What is this so-called "execution queue" and why do you think it's possible to remove things from it? – Alnitak Aug 22 '12 at 13:20
  • "execution queue" might not be the best expression for what I mean. I mean the function is called asynchronously and must wait until the next possible execution point, allowing the current function to finish. Maybe invocation queue is a better expression – Asciiom Aug 22 '12 at 13:21
  • that would make some sense if you had written "event queue" rather than "execution queue", except that it's in no way possible for `setTimeout` to _remove_ something from the event queue. – Alnitak Aug 22 '12 at 13:29
  • Poor formulation because of trying to be too quick. I rephrased. – Asciiom Aug 22 '12 at 13:33