Last active
July 25, 2019 03:29
-
-
Save jumplee/e95897e98f477637ea2e902ebd1d7099 to your computer and use it in GitHub Desktop.
执行某个函数n次并有一定时间间隔
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function clock(func,times,timeout){ | |
if(!timeout||parseInt(timeout)===NaN){ | |
timeout=100; | |
} | |
var _counter=0; | |
function _(){ | |
func(); | |
_counter++; | |
if(_counter<times){ | |
setTimeout(function(){ | |
_(); | |
},timeout); | |
} | |
} | |
_(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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