Last active
January 6, 2016 19:13
-
-
Save DukeBaird/535d787f745ef5cef3ce to your computer and use it in GitHub Desktop.
Wait for
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// From phantomjs | |
function waitFor(testFx, onReady, timeOutMillis) { | |
var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 300000, // maxtimeout is 3m afaik | |
start = new Date().getTime(), | |
condition = false, | |
interval = setInterval(function() { | |
if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { | |
// If not time-out yet and condition not yet fulfilled | |
condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code, reruns testFx to see if its good now | |
} else { | |
if(!condition) { | |
// If condition still not fulfilled (timeout but condition is 'false') | |
console.log("'waitFor()' timeout"); | |
phantom.exit(1); | |
} else { | |
// Condition fulfilled (timeout and/or condition is 'true') | |
console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); | |
typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled | |
clearInterval(interval); //< Stop this interval | |
} | |
} | |
}, 1000); //< repeat check every 1000ms | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment