Function.prototype.waitUntil = function (condition, interval) {
interval = interval || 100;
var fn = this,
shell = function () {
var timer = setInterval(
function () {
var check;
try { check = !!(condition()); } catch (e) { check = false; }
if (check) {
clearInterval(timer);
delete timer;
fn();
}
},
interval
);
};
return shell;
};
(function () { window.status = 'I waited, and waited, and waited...'; }).waitUntil(function () { return !!(something === true); })();
Refactorings
No refactoring yet !
tjholowaychuk
June 1, 2010, June 01, 2010 15:32, permalink
here is a nodejs example
/**
* Module dependencies.
*/
var sys = require('sys');
Function.prototype.waitUntil = function(fn, interval) {
var id = setInterval(function(self){
if (fn()) {
clearInterval(id);
self();
}
}, interval, this);
};
var done;
(function(){
sys.puts('... complete!');
}).waitUntil(function(){
sys.puts('... incomplete');
return done;
}, 200);
setTimeout(function(){
done = true;
}, 1000);
Talk me out of using this...