casperjs: Immediately check the condition in waitFor and don't wait after the condition is true

Normally, casper delays checking the waitFor condition for 100 milliseconds and
further does not act on that check for another 100 milliseconds.  This is just
silly.

(imported from commit ad046ceda81abda5c609ce25ef0d4fb27d3da716)
This commit is contained in:
Zev Benjamin
2014-03-07 15:24:04 -05:00
committed by Steve Howell
parent 39f632134d
commit 5fd58cf249

View File

@@ -1708,14 +1708,34 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
} }
return this.then(function _step() { return this.then(function _step() {
this.waitStart(); this.waitStart();
var self = this;
var interval;
var start = new Date().getTime(); var start = new Date().getTime();
var condition = false; var condition = testFx.call(self, self);
var interval = setInterval(function _check(self, testFx, timeout, onTimeout) { function _waitFinished () {
if ((new Date().getTime() - start < timeout) && !condition) { self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info");
condition = testFx.call(self, self); if (then) {
} else { then.call(self, self);
}
}
if (condition) {
self.waitDone(); self.waitDone();
_waitFinished();
return;
}
interval = setInterval(function _check(testFx, timeout, onTimeout) {
var timedOut = (new Date().getTime() - start > timeout);
if (!timedOut && !condition) {
condition = testFx.call(self, self);
if (!condition) { if (!condition) {
return;
}
}
// Either we timed out or the condition is true
self.waitDone();
clearInterval(interval);
if (timedOut) {
self.log("Casper.waitFor() timeout", "warning"); self.log("Casper.waitFor() timeout", "warning");
self.emit('waitFor.timeout'); self.emit('waitFor.timeout');
var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout; var onWaitTimeout = onTimeout ? onTimeout : self.options.onWaitTimeout;
@@ -1724,14 +1744,9 @@ Casper.prototype.waitFor = function waitFor(testFx, then, onTimeout, timeout) {
} }
onWaitTimeout.call(self, timeout); onWaitTimeout.call(self, timeout);
} else { } else {
self.log(f("waitFor() finished in %dms.", new Date().getTime() - start), "info"); _waitFinished();
if (then) {
then.call(self, self);
} }
} }, 100, testFx, timeout, onTimeout);
clearInterval(interval);
}
}, 100, this, testFx, timeout, onTimeout);
}); });
}; };