gpt4 book ai didi

javascript - 点击事件丢失?

转载 作者:行者123 更新时间:2023-11-30 10:36:38 29 4
gpt4 key购买 nike

我有一个包含两种类型事件的网页 (JavaScript):

  • Canvas 中的点击事件(仅显示提醒)。
  • 每秒定期事件(由计时器触发)。

周期性事件忙 200 - 300 毫秒(它正在启动同步 SOAP 调用)。

Canvas 事件:

var c=document.getElementById("myCanvas");
c.addEventListener('click', function(e) {alert('Click');},false);

周期性的:

var timer1= new Timer(1000,"Refresh()"); // A library call


function Refresh()
{
timer1.stop();

// Synchronous SOAP call would come here

// with the results we draw the canvas again

timer1.start();
}

如果我在进行 SOAP 调用时单击 Canvas ,则不会触发单击事件,或者我认为是因为未显示警报。

如果我在周期性事件完成后点击一次,然后再调用它,则每次点击都会显示警报。

据我所知,如果click事件是在periodic event正在执行的时候产生的,应该会延迟到另一个执行完。

可能导致问题的原因是什么?

PD我正在使用 Firebug(在 Firefox 上)来了解何时进行 SOAP 调用。

最佳答案

同步调用会阻塞 I/O,因此不会触发警报。由于您正在等待电话回电,因此在此期间发生的任何操作都将被忽略。

这篇文章可能会有所帮助:http://ejohn.org/blog/how-javascript-timers-work/

Since all JavaScript in a browser executes on a single thread asynchronous events (such as mouse clicks and timers) are only run when there's been an opening in the execution.

  • JavaScript engines only have a single thread, forcing asynchronous events to queue waiting for execution.
  • setTimeout and setInterval are fundamentally different in how they execute asynchronous code.
  • If atimer is blocked from immediately executing it will be delayed until the next possible point of execution (which will be longer than the desired delay).
  • Intervals may execute back-to-back with no delay if they take long enough to execute (longer than the specified delay).

关于javascript - 点击事件丢失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13647977/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com