forked from mozilla/persona
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
23 lines (23 loc) · 901 Bytes
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// wait for something to work.
// poll - how often to check in ms
// timeout - how long to try before giving up in ms
// check - a function that will perform the check, accepts a callback where the
// first argument is a boolean indicating whether the check was successful
// (if false, the check will be retried)
// complete - a callback to invoke upon timeout or successful check with args.slice(1)
// from the check function
exports.waitFor = function (poll, timeout, check, complete) {
var startTime = new Date();
function doit() {
check(function(done) {
if (!done && ((new Date() - startTime) > timeout)) {
complete.call(null, "timeout hit");
} else if (done) {
complete.apply(null, Array.prototype.slice.call(arguments, 1));
} else {
setTimeout(doit, poll);
}
});
}
setTimeout(doit, poll);
};