Files
node/Nodejs-Socketio-Mysql-Demo/node_modules/wd/lib/browser-scripts/wait-for-cond-in-browser.js
2012-05-25 09:03:56 -04:00

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);