/* global casper, module */
//**********************************************************************
// function waitfor - waiting for a condition to met (synchronously)
//
// Parameters:
// test: test function that returns something
// expectedValue: the expected return value
// msec: delay between retries
// count: initial loop count (must be 0)
// maxcount: maximum loop count
// callback: callback function
//**********************************************************************
function waitfor(test, expectedValue, msec, count, maxcount, callback) {
expectedValue = typeof expectedValue !== \'undefined\' ? expectedValue : true;
msec = typeof msec !== \'undefined\' ? msec : 100;
count = typeof count !== \'undefined\' ? count : 0;
maxcount = typeof maxcount !== \'undefined\' ? maxcount : 50;
// Check if condition met. If not, re-check later (msec)
while ((test() !== expectedValue) && (count < maxcount)) {
sleep(msec);
count++;
waitfor(test, expectedValue, msec, count, maxcount, callback);
return;
}
// callback is executed in any case, even if condition is not met
if (callback) callback();
}
function sleep(ms) {
var unixtime_ms = new Date().getTime();
while (new Date().getTime() < unixtime_ms + ms) {}
}
module.exports = {
waitfor: waitfor
};