Recursive approach:
function multiTimeoutCall (callback, delay, times) {
if (times > 0){
setTimeout(function () {
callback();
multiTimeoutCall (callback, delay, times - 1);
}, delay);
}
}
Using:
multiTimeoutCall (toggle, 100, 4);
Edit: Another approach without populating the call stack:
function multiTimeoutCall (callback, delay, times) {
setTimeout(function action() {
callback();
if (--times > 0) {
setTimeout (action, delay);
}
}, delay);
}
I could use arguments.calleefunctions instead of the named expression, but it seems that it will become obsolete someday in ECMAScript 5 ...
source
share