Link1 Link2​ $('#link...">

Concurrency - does jquery handler save queue?

I have this easy code :

<a href="#" id="link1">Link1</a> <a href="#" id="link2">Link2</a>​ $('#link1').click(function () { alert("First"); }); $('#link2').click(function () { alert("Second"); }); ​ 

in the "remote" function, which the user clicks on Link1 (first), and not on Link2, with a difference of 0.000000001 ms (I know this is impossible in reality, but it only needs to be known), it is possible that the warning # link2 starts before the warning about # link1?

And if so, what decision to block the handler # link2 before the action # link1 is not complete?

0
source share
1 answer

"... is it possible that warning #link2 starts before warning #link1 ?"

Not. If clicks do occur in this order, events will fire in that order.

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

If asynchronous code is entered into the handler for the first click, the first click will still start first, but the second click may begin if it is executed before the first asynchronous code is run.


To simulate a super-fast click sequence, simply enter the code to trigger events.

 var l1 = $('#link1'); var l2 = $('#link2'); l1.click(); // this alert will always come first l2.click(); 
+3
source

Source: https://habr.com/ru/post/1399143/


All Articles