lib/phantom/modules/selectorDetect.js
function waitFor (testFx, onReady, onTimeout, timeout, checkInterval) {
  var condition = false,
  interval = setInterval(function () {
    if ( (new Date().getTime() - start < timeout) && !condition ) {
      // If not timeout yet and condition not yet fulfilled
      condition = testFx();
    } else {
      clearInterval(interval); // Stop this interval
      if ( !condition ) {
        // If condition still not fulfilled (timeout but condition is 'false')
        onTimeout();
      } else {
        // Condition fulfilled (timeout and/or condition is 'true')
        onReady((new Date().getTime() - start));
      }
    }
  }, checkInterval);
}
function waitFor (testFx, onReady, onTimeout, timeout, checkInterval) {
  var condition = false,
  interval = setInterval(function () {
    if ( (new Date().getTime() - start < timeout) && !condition ) {
      // If not timeout yet and condition not yet fulfilled
      condition = testFx();
    } else {
      clearInterval(interval); // Stop this interval
      if ( !condition ) {
        // If condition still not fulfilled (timeout but condition is 'false')
        onTimeout();
      } else {
        // Condition fulfilled (timeout and/or condition is 'true')
        onReady((new Date().getTime() - start));
      }
    }
  }, checkInterval);
}
waitFor

Heavily borrowed from phantomJS 'waitFor' example