mirror of
https://github.com/sstent/node.git
synced 2026-01-27 15:41:43 +00:00
33 lines
780 B
JavaScript
33 lines
780 B
JavaScript
// run in the browser
|
|
|
|
//parse arguments
|
|
var condExpr = arguments[0], timeout = arguments[1],
|
|
poll = arguments[2], cb = arguments[3];
|
|
|
|
// recursive implementation
|
|
waitForConditionImpl = function(conditionExpr, limit, poll, cb) {
|
|
|
|
// timeout check
|
|
if (Date.now() < limit) {
|
|
// condition check
|
|
res = eval(conditionExpr);
|
|
if (res == true ) {
|
|
// condition ok
|
|
return cb(res);
|
|
} else {
|
|
// wait for poll and try again
|
|
setTimeout(function() {
|
|
waitForConditionImpl(conditionExpr, limit, poll, cb);
|
|
}, poll);
|
|
}
|
|
} else {
|
|
// try one last time
|
|
res = eval(conditionExpr);
|
|
return cb(res);
|
|
}
|
|
};
|
|
|
|
// calling impl
|
|
limit = Date.now() + timeout;
|
|
waitForConditionImpl(condExpr, limit, poll, cb);
|