a |
var defer = (function(){ //wrapped in IIFE to provide a scope for deferreds and wrap var running = false; var deferreds = [function(){alert(1)},function(){alert(2)}]; function wrap(func){ return function(){ func(); var next = deferreds.shift(); if(next){ setTimeout(wrap(next),0); }else{ running = false; } } } return function(func){ if(running){ deferreds.push(func); }else{ setTimeout(wrap(func),0); running = true; } } })()
|
pending… |
b |
function inOrderTimeout(/* func1, [func2, func3, ...funcN,] timeout */) { var id, args = arguments, numToRun = args.length - 1; /*if (numToRun < 1) { throw new Error('You did not provide at least 1 function to run for inOrderTimeout().\nCorrect syntax is as follows:\n\ninOrderTimeout(func1, [func2, func3, ...funcN] timeout)'); return; } var currentFunc = 0, timeout = args[numToRun]; //array index is the same as the number of functions to run if (typeof timeout !== 'number' && timeout !== '0' && +timeout === 0 || timeout === Infinity || timeout === -Infinity || +timeout !== +timeout) { throw new Error('You did not provide a valid number, or it was equal to positive or negative Infinity. Only integers and string representations of integers (without anything else in it) are permitted.'); return; }*/ (function caller(func, timeout) { if (currentFunc > numToRun - 1) { clearTimeout(id); return; } id = setTimeout(function () { func(); caller(args[++currentFunc], timeout); }, Math.floor(timeout)); }(args[currentFunc], timeout)); } inOrderTimeout(function(){alert(1)},function(){alert(2)},0)
|
pending… |
0 comments