首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果函数在javascript中有执行延迟,则停止该函数

问如果函数在javascript中有执行延迟,则停止该函数
EN

Stack Overflow用户
提问于 2020-04-15 13:30:03
回答 1查看 71关注 0票数 0

在我的场景中,我有一个鼠标事件。当我滚动鼠标时,它点击了一个函数,相应的函数就会执行。我正在检查函数的执行时间,如果函数的执行时间超过了一定的时间,我想要停止函数。不使用setInterval/ setTimeOut,这是可能的吗!

提前感谢

EN

回答 1

Stack Overflow用户

发布于 2020-04-15 15:04:38

考虑到这一点:我们应该更好地检查它是否真的删除了事件(在jsfiddle上,我目前对此持怀疑态度)

像这样的东西合适吗?

html:

代码语言:javascript
复制
<div>
<p id="checks">
Waiting action
</p>
</div>

<div id="scroller">

In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 
In my scenario, I have a mouse event. Where when I scroll the mouse everything it hits a function and respective function executes. I'm looking to check the execution time of the function and when If the execution time of the function exceeds a certain time, I want to stop the function. Is that possible without using setInterval/ setTimeOut! 

</div>

Js:

代码语言:javascript
复制
var checks = document.getElementById('checks');
var scroller = document.getElementById('scroller');

scroller.addEventListener("scroll", () => {

   checks.innerHTML = "on scrolling";
   var scrolltimer = setInterval(myTimer, 1000);
   function myTimer()
   {
     checks.innerHTML = "stopping...";
     stopwork(scrolltimer,scroller);
   }

 },true);


function stopwork(scrolltimer,scroller)
{
  scroller.removeEventListener("scroll", null);
  clearTimeout(scrolltimer);
  checks.innerHTML = "stopped";
  return false;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61221927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档