B2G/gecko/docshell/test/chrome/docshell_helpers.js
/**
 * Helper function which waits until another function returns true, or until a 
 * timeout occurs, and then notifies a callback.
 *
 * Parameters:
 *
 *    fn: a function which is evaluated repeatedly, and when it turns true, 
 *        the onWaitComplete callback is notified.
 *
 *    onWaitComplete:  a callback which will be notified when fn() returns 
 *        true, or when a timeout occurs.
 * 
 *    timeout: a timeout, in seconds or ms, after which waitForTrue() will 
 *        fail an assertion and then return, even if the fn function never 
 *        returns true.  If timeout is undefined, waitForTrue() will never 
 *        time out.
 */
Helper function which waits until another function returns true, or until a
timeout occurs, and then notifies a callback.



a function which is evaluated repeatedly, and when it turns true,
the onWaitComplete callback is notified.

a callback which will be notified when fn() returns
true, or when a timeout occurs.

a timeout, in seconds or ms, after which waitForTrue() will
fail an assertion and then return, even if the fn function never
returns true.  If timeout is undefined, waitForTrue() will never
time out.
Helper function which waits until another function returns true or until a timeout occurs and then notifies a callback . a function which is evaluated repeatedly and when it turns true the onWaitComplete callback is notified . a callback which will be notified when fn () returns true or when a timeout occurs . a timeout in seconds or ms after which waitForTrue () will fail an assertion and then return even if the fn function never returns true . If timeout is undefined waitForTrue () will never time out .
function waitForTrue(fn, onWaitComplete, timeout) {
  var start = new Date().valueOf();
  if (typeof(timeout) != "undefined") {
    if (timeout < 500)
      timeout *= 1000;
  }
  var intervalid;
  intervalid =
    setInterval(
      function() {  
        var timeoutHit = false;
        if (typeof(timeout) != "undefined") {
          timeoutHit = new Date().valueOf() - start >= 
            timeout ? true : false;
          if (timeoutHit) {
            ok(false, "Timed out waiting for condition");
          }
        }
        if (timeoutHit || fn.call()) {
          clearInterval(intervalid);
          onWaitComplete.call();          
        } 
      }, 20);
}
function waitForTrue ( fn , onWaitComplete , timeout ) { var start = new Date ( ) . valueOf ( ) ; if ( typeof ( timeout ) != "undefined" ) { if ( timeout < 500 ) timeout *= 1000 ; } var intervalid ; intervalid = setInterval ( function ( ) { var timeoutHit = false ; if ( typeof ( timeout ) != "undefined" ) { timeoutHit = new Date ( ) . valueOf ( ) - start >= timeout ? true : false ; if ( timeoutHit ) { ok ( false , "Timed out waiting for condition" ) ; } } if ( timeoutHit || fn . call ( ) ) { clearInterval ( intervalid ) ; onWaitComplete . call ( ) ; } } , 20 ) ; }
Helper function which waits until another function returns true, or until a
 timeout occurs, and then notifies a callback.
Helper function which waits until another function returns true or until a timeout occurs and then notifies a callback .
// If timeoutWait is less than 500, assume it represents seconds, and  // convert to ms. // Loop until the test function returns true, or until a timeout occurs, // if a timeout is defined. // Stop calling the test function and notify the callback.