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;
This is a workaround for a peculiarity of IE6 and IE7 which can retrieve an AJAX result from cache without firing the
XMLHTTPRequestcallback, immediately setting itsreadyStateproperty to4, instead.However the API contract for
$.ajaxrequires that it return immediately for an asynchronous request (i.e. without calling the programmer-supplied callbacks).Hence
$.ajaxcall tests for those cached results, and then fakes the required asynchronous callback usingsetTimeout.The
$.ajaxcall 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.