简体   繁体   English

多个onClick选择器如何按时工作?

[英]How exactly do multiple onClick selectors work timingwise?

Currently, the code attaching the onClick events looks like this: 当前,附加onClick事件的代码如下所示:

 $("#LieblingsButton").click(function(){ reactivateFavSeatCheck() }) $("#LieblingsButton").click(function(){ checkForWeekReservationByFavSeatButton() }) $("#LieblingsButton").click(function(){ fetchDataFromDatabase() }) $("#LieblingsButton").click(function(){ executeReservation() }) 

fetchDataFromDatabase() does some async work, but this is already taken care of by async/await and promises. fetchDataFromDatabase()做一些异步工作,但是异步/等待和诺言已经解决了这一问题。 executeReservation() shall ONLY start if fetchDataFromDatabase() has finished its execution. 只有在fetchDataFromDatabase()完成执行后, executeReservation()启动。

Currently, everything is working. 目前,一切正常。 But I fear that this might only be the case because the cirumcstances allow for it. 但是我担心可能只是这样,因为事态允许。 What if fetchDataFromDatabase() takes a few ms "too long"? 如果fetchDataFromDatabase()花费几毫秒“太长”怎么办?

I already learned that when you add multiple event handlers to an element via jquery (how about with native JS?), they fire in the order which you have determined in your code. 我已经了解到,当您通过jquery向元素添加多个事件处理程序时(使用本机JS怎么样?),它们将按照您在代码中确定的顺序触发。 But I dont know if this "rule" also encompasses that Event2 will only fire if Event1 has already finished execution? 但是我不知道该“规则”是否还包含Event2仅在Event1已经完成执行时才会触发吗?

And besides, does the following code the same as the code above (from a functional perspective)? 此外,以下代码是否与上面的代码相同(从功能角度而言)?

 $("#LieblingsButton").click(function(){ reactivateFavSeatCheck() checkForWeekReservationByFavSeatButton() fetchDataFromDatabase() executeReservation() }) 

First thing's first: JS execution is single-threaded in nature. 首先是第一件事:JS执行本质上是单线程的。 No matter how asynchronous your code appears to be, only one part of it is running at any given time. 无论您的代码看起来多么异步,在任何给定时间都只有一部分运行。 You can read up more about this here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop 您可以在此处阅读有关此内容的更多信息: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop

Secondly, event listeners are triggered in a loop, in order of when they were attached. 其次,事件侦听器按照连接的时间顺序循环触发。 You can think of it sort of like this: 您可以这样想:

handlers.forEach((handler) => {
  try { handler(event); } catch (e) { /* put warning in console */ }
});

But I fear that this might only be the case because the cirumcstances allow for it. 但是我担心可能只是这样,因为事态允许。 What if fetchDataFromDatabase() takes a few ms "too long"? 如果fetchDataFromDatabase()花费几毫秒“太长”怎么办?

With the following test, you can observe how a while loop in the first event listener stops the second one from firing, hence confirming your suspicion. 通过以下测试,您可以观察到第一个事件侦听器中的while循环如何阻止第二个事件监听器触发,从而证实您的怀疑。 Note: I did not embed it as a snippet because snippets overriding of console somehow broke this example. 注意:我没有将其作为代码段嵌入,因为覆盖控制台的代码段以某种方式破坏了此示例。 Just paste it in your browser console. 只需将其粘贴到浏览器控制台中即可。

// $(e).click(fn) roughly equals e.addEventListener('click', fn);

window.addEventListener('test', () => {
  console.log('first handler', new Date());
  const time = Date.now();
  while (Date.now() - time < 2000) {}
});

window.addEventListener('test', () => {
  console.log('second handler', new Date());
});

window.dispatchEvent(new Event('test'));

However... 然而...

If you are doing work asynchronously, things get much better. 如果您异步进行工作,情况会好得多。

 window.addEventListener('test', () => { setTimeout(() => { console.log('first handler'); }, 1000); }); window.addEventListener('test', () => { console.log('second handler'); }); window.dispatchEvent(new Event('test')); 

With this example you can see that although the first event handler schedules a timer, this does not block the next event listeners from running. 通过此示例,您可以看到尽管第一个事件处理程序安排了计时器,但这不会阻止下一个事件侦听器运行。 The same is true if you were to say, make an XHR request. 如果要说XHR请求,也是如此。

So finally , armed with this information, we can say that it is actually better to use a single event listener, like in your second snippet. 因此,最后 ,有了这些信息,我们可以说实际上最好使用单个事件侦听器,就像在第二个片段中一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM