并发 - jquery handler是否保留队列? [英] Concurrency - does jquery handler preserve queue?

查看:106
本文介绍了并发 - jquery handler是否保留队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的验证码

<a href="#" id="link1">Link1</a>
<a href="#" id="link2">Link2</a>​

$('#link1').click(function () {
    alert("First");
});

$('#link2').click(function () {
    alert("Second");    
});    
​



<比Link2上的差别为0,000000001毫秒(我知道,这是不可能在现实中,但只是知道),#link2的警报可能在#link1的警报之前开始?

in a "remote" possiblity that a user click on Link1 (first) and than on Link2, with a difference of 0,000000001 ms (I know, is it impossible in the reality, but is just to know), is it possible that the alert of #link2 start before the alert of #link1?

如果是,#link2的块处理程序的解决方案,直到#link1的操作没有完成?

And if yes, what's the solution to block handler of #link2 till the action of #link1 is it not finished?

推荐答案


...可能是#link2 #link1 的提醒?

"...is it possible that the alert of #link2 start before the alert of #link1?"

JavaScript是单线程的,事件系统是同步的 >。这保证了第一次点击的同步部分必须在允许第二次点击事件之前完成。

JavaScript is single threaded, and the event systems are synchronous. This guarantees that the synchronous portion of the first click must complete before the second click event is allowed.

如果异步代码被引入到第一次点击的处理程序中,则第一次点击仍然会首先开始,但如果第二次点击在第一次点击的异步代码运行之前就开始。

If asynchronous code is introduced into the handler for the first click, then the first click will still begin first, but the second click could start if it takes place before the first click's asynchronous code runs.

要模拟超快速的点击次数,只需让代码触发事件即可。

To simulate a super fast sequence of clicks, just have the code trigger the events.

var l1 = $('#link1');
var l2 = $('#link2');

l1.click();  // this alert will always come first
l2.click();

这篇关于并发 - jquery handler是否保留队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆