English 中文(简体)
多场活动听众使用“彩虹” 同一活动的清单[复制]
原标题:multiple event listeners using addeventListener to the same event [duplicate]

我将这一法典归入浏览器:

document.body.addEventListener("click", () => {
    Promise.resolve().then(() => console.log(1));
    console.log(2);
}, false);

document.body.addEventListener("click", () => {
    Promise.resolve().then(() => console.log(3));
    console.log(4);
}, false);

这两场活动听众聆听了点击事件和打印数字。

接着,我写了执行这一声明的一条线:

document.body.click();

其结果是:

2
4
1
3

但随后,我点击了屏幕,因此触发了事件警报:

2
1
4
3

为什么在屏幕上点击和执行<代码> 。

问题回答

你以点击或通过该守则加以操作的方式看待不同结果的原因,是合成业务是如何运作的,特别是许诺。

When you manually click the screen, the event listeners attached to the click event of the body element are triggered in the order they were added in your code. In this case, the first listener executes, logs 2, then 1, and then the second listener executes, logging 4 and 3 respectively.

2
1
4
3

Running document.body.click(): When you execute document.body.click(), the click event is triggered programmatically, but since you re also using promises (Promise.resolve().then(...)), the behavior slightly differs.

The event listeners attached to the click event are still executed in the order they were added. However, due to the promises, the callbacks inside them (logging 1, 2, 3, 4) might not execute immediately, as they are queued in the microtask queue. This means that while the click event is fired immediately when you call document.body.click(), the promises inside the event listeners are resolved asynchronously, and their callbacks (console.log statements) are executed later. So, the asynchronous nature of promises causes the difference in timing between manual and programmatic clicks, resulting in the different order of logged numbers.

我建议检查:microtasks and pledges





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签