在我的场景中,我有一个鼠标事件。当我滚动鼠标时,它点击了一个函数,相应的函数就会执行。我正在检查函数的执行时间,如果函数的执行时间超过了一定的时间,我想要停止函数。不使用setInterval/ setTimeOut,这是可能的吗!
提前感谢
发布于 2020-04-15 15:04:38
考虑到这一点:我们应该更好地检查它是否真的删除了事件(在jsfiddle上,我目前对此持怀疑态度)
像这样的东西合适吗?
html:
<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:
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;
}https://stackoverflow.com/questions/61221927
复制相似问题