简体   繁体   English

在连续调用的函数中的setTimeout

[英]setTimeout in function that is continually called

I have a function that is called when a user drags with the mouse. 我有一个用户拖动鼠标时调用的函数。 What the function does is not relevant but it is called loadTiles(); 该函数的作用与此无关,但它被称为loadTiles();

What I want is for the function to be called every 1000ms during user drag. 我想要的是在用户拖动期间每1000ms调用一次该函数。 However, if I do the following, the setTimeout will be started continually throughout the drag: 但是,如果我执行以下操作,则会在整个拖动过程中不断启动setTimeout:

window.ontouchmove = function(){
  setTimeout(function(){ loadTiles(); },1000);
};

Is there a way to have the setTimeout actually count down the 1000ms uninterrupted and keep resetting to count again like setInterval ? 有没有办法让setTimeout真正倒数1000毫秒不间断,并保持重置计数再次像setInterval

You could set a Boolean variable to see if its been run or not. 您可以设置一个布尔变量以查看它是否已运行。

var isrun=false;
window.ontouchmove = function(){
if(isrun==false){
isrun=true;
setTimeout(function(){ loadTiles(); },1000);
}
};

then in your loadTiles function set isrun back to false at the end 然后在你的loadTiles函数集中,isrun最后返回false

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

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