Skip to content

Instantly share code, notes, and snippets.

@yukidarake
Created February 6, 2012 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukidarake/1751139 to your computer and use it in GitHub Desktop.
Save yukidarake/1751139 to your computer and use it in GitHub Desktop.
node.jsのテストで使えるかと思って出来心で書いてみた。
function loop(fn, times, delay) {
if (!times || times < 0) {
throw new Error('illegal parameter times', times);
}
if (!delay || delay < 0) {
throw new Error('illegal parameter delay', delay);
}
function _loop(n) {
if (n === times) {
return;
}
fn(n);
setTimeout(function() {
_loop(n + 1);
}, delay);
}
_loop(0);
}
// 合計50回、10ms空けて
loop(function(i) {
console.log(i);
}, 50, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment