document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /* global casper, module */
  2.  
  3. //**********************************************************************
  4. // function waitfor - waiting for a condition to met (synchronously)
  5. //        
  6. // Parameters:
  7. //    test: test function that returns something
  8. //    expectedValue: the expected return value
  9. //    msec: delay between retries
  10. //    count: initial loop count (must be 0)
  11. //    maxcount: maximum loop count
  12. //    callback: callback function
  13. //**********************************************************************
  14. function waitfor(test, expectedValue, msec, count, maxcount, callback) {
  15.     expectedValue = typeof expectedValue !== \'undefined\' ? expectedValue : true;
  16.     msec = typeof msec !== \'undefined\' ? msec : 100;
  17.     count = typeof count !== \'undefined\' ? count : 0;
  18.     maxcount = typeof maxcount !== \'undefined\' ? maxcount : 50;
  19.     // Check if condition met. If not, re-check later (msec)
  20.     while ((test() !== expectedValue) && (count < maxcount)) {
  21.         sleep(msec);
  22.         count++;
  23.         waitfor(test, expectedValue, msec, count, maxcount, callback);
  24.         return;
  25.     }
  26.     // callback is executed in any case, even if condition is not met
  27.     if (callback) callback();
  28. }
  29.  
  30. function sleep(ms) {
  31.     var unixtime_ms = new Date().getTime();
  32.     while (new Date().getTime() < unixtime_ms + ms) {}
  33. }
  34.  
  35. module.exports = {
  36.     waitfor: waitfor
  37. };
');