weixin_33737134 2012-08-22 13:03 采纳率: 0%
浏览 102

jQuery ajax setTimeout 0毫秒[重复]

This question already has answers here:
                </div>
            </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2012-08-23 06:31:08Z" class="relativetime">7 years ago</span>.</div>
        </div>
    </aside>

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;
</div>
  • 写回答

2条回答 默认 最新

  • 零零乙 2012-08-22 13:05
    关注

    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.

    评论

报告相同问题?