Last active
August 29, 2015 14:05
-
-
Save nauzilus/f88f20e1de581d2fe8eb to your computer and use it in GitHub Desktop.
setLimitedTimeout
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() { | |
if (typeof window.setLimitedTimeout === 'undefined' && typeof window.clearLimitedTimeout === 'undefined') { | |
var timers={},_id=0; | |
window.setLimitedTimeout = function(test, reps, interval, cb) { | |
var attempt = 0; | |
var intFunc = typeof interval === 'function' | |
? interval | |
: function(attempt) { return interval; }; | |
var id = _id++; | |
timers[id] = setTimeout(function doit() { | |
delete timers[id]; | |
if (attempt >= reps) { | |
cb && cb(false, attempt); | |
} | |
else if (test(++attempt) === true) { | |
cb && cb(true, attempt); | |
} | |
else { | |
timers[id] = setTimeout(doit, intFunc(attempt)); | |
} | |
}, intFunc(attempt)); | |
return id; | |
}; | |
window.clearLimitedTimeout = function(id) { | |
clearTimeout(timers[id]); | |
delete timers[id]; | |
}; | |
} | |
})(); | |
// mininal version (no callback, no attempt # passed to functions, no interval function support) | |
/*var setLimitedTimeout = function(test, reps, interval) { | |
setTimeout(function doit() { | |
if (reps-- > 0 && test() === false) { | |
setTimeout(doit, interval); | |
} | |
}, interval); | |
}*/ |
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
// Example: making sure SP _spBodyOnLoadWrapper() has a chance to be called naturally, else call ourselves after 10 seconds | |
setLimitedTimeout( | |
function() { return !!_spBodyOnLoadCalled; }, | |
100, | |
100, | |
function(success) { | |
success || _spBodyOnLoadWrapper(); | |
console.log("SP ready to go"); | |
} | |
) | |
// Example: simply repeat 10 times with 100ms delay (similar to http://stackoverflow.com/a/10250983) | |
setLimitedTimeout( | |
function(i) { console.log("itr", i); }, | |
10, | |
100 | |
) | |
// Example: interval as a function, "backing-off" over time | |
setLimitedTimeout( | |
function(i) { console.log("itr", i); }, | |
25, | |
function(i) { return i * 50; } | |
) | |
window.foo = true; | |
setLimitedTimeout( | |
function(itr) { | |
window.foo = false; | |
throw "Shouldn't see this"; | |
return true; | |
}, | |
0, | |
100, | |
function(earlyExit, finalAttempt) { | |
console.log(earlyExit === false && finalAttempt === 0 && window.foo === true ? "PASS" : "FAIL", "no-op", earlyExit, finalAttempt) | |
} | |
) | |
setLimitedTimeout( | |
function(itr) { return true; }, | |
10, | |
100, | |
function(earlyExit, finalAttempt) { | |
console.log(earlyExit === true && finalAttempt === 1 ? "PASS" : "FAIL", "early exit", earlyExit, finalAttempt) | |
} | |
) | |
setLimitedTimeout( | |
function(itr) { return false; }, | |
10, | |
100, | |
function(earlyExit, finalAttempt) { | |
console.log(earlyExit === false && finalAttempt === 10 ? "PASS" : "FAIL", "exceed reps", earlyExit, finalAttempt) | |
} | |
) | |
setLimitedTimeout( | |
function(itr) { return itr === 10; }, | |
10, | |
100, | |
function(earlyExit, finalAttempt) { | |
console.log(earlyExit === true && finalAttempt === 10 ? "PASS" : "FAIL", "on last", earlyExit, finalAttempt) | |
} | |
) | |
setLimitedTimeout( | |
function(itr) { return itr === 10; }, | |
10, | |
100, | |
function(earlyExit, finalAttempt) { | |
console.log(earlyExit === true && finalAttempt === 10 ? "PASS" : "FAIL", "on last", earlyExit, finalAttempt) | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment