CaribouCode
CaribouCode

Reputation: 14398

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();

What I want is for the function to be called every 1000ms during user drag. However, if I do the following, the setTimeout will be started continually throughout the drag:

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?

Upvotes: 0

Views: 77

Answers (1)

bensonsearch
bensonsearch

Reputation: 312

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

Upvotes: 2

Related Questions