Are event handlers guaranteed to complete before AJAX callbacks are invoked?

后端 未结 4 775
面向向阳花
面向向阳花 2020-12-31 11:21

Suppose I have a event handler which makes two AJAX calls to the server:

$(\"#foo\").click(function(){ 
    $.get(\"bar\", function(){ alert(\"Hello\"); });
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-31 12:02

    Yes, JavaScript is single threaded, so your execution will never get preempted.

    Asynchronous callbacks and events work the same way; your handler for mousedown is guaranteed to finish before your handler for a mouseup, even if your mousedown handler takes 2 seconds and you let the mouse go immediately.

    The same goes for an AJAX callback, it gets put into the same (kind of) queue as events waiting to be processed

提交回复
热议问题