1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/** * 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...