Skip to content

Instantly share code, notes, and snippets.

@jumplee
Last active July 25, 2019 03:29
Show Gist options
  • Save jumplee/e95897e98f477637ea2e902ebd1d7099 to your computer and use it in GitHub Desktop.
Save jumplee/e95897e98f477637ea2e902ebd1d7099 to your computer and use it in GitHub Desktop.
执行某个函数n次并有一定时间间隔
function clock(func,times,timeout){
if(!timeout||parseInt(timeout)===NaN){
timeout=100;
}
var _counter=0;
function _(){
func();
_counter++;
if(_counter<times){
setTimeout(function(){
_();
},timeout);
}
}
_();
}
function clockWithIndex(func,times,timeout){
if(!timeout||parseInt(timeout)===NaN){
timeout=100;
}
func(0);
var _counter=1;
var _p=setInterval(function(){
func(_counter);
_counter++;
if(_counter>=times){
clearInterval(_p);
}
},timeout);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment