JQuery mousdown with setInterval endless

随声附和 提交于 2019-12-12 03:24:16

问题


My question is referring onto this question.

At the moment I am using this answer, but I still can trick that code, so the clearInterval won't stop. While holding my left mouse button I simple press the right one to open the contextmenu. Now the clearInterval wasn't called.

If I add the contextmenu event to the clicker, with a clearInterval it does get called, but it won't clear the interval.

See this demo


回答1:


Add clearInterval() to the mousedown event:

clicker.mousedown(function() {
  clearInterval(timeout);
  timeout = setInterval(function() {
    clicker.text(count++);
  }, 500);

  return false;
});

That will be triggered on the context menu, and it will prevent multiple timers from running at once.

Fiddle



来源:https://stackoverflow.com/questions/36316638/jquery-mousdown-with-setinterval-endless

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!