Created
February 6, 2012 09:54
-
-
Save yukidarake/1751139 to your computer and use it in GitHub Desktop.
node.jsのテストで使えるかと思って出来心で書いてみた。
This file contains 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 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