Created
January 31, 2015 22:53
-
-
Save au5ton/6cc4b1db71dd535a8d1f to your computer and use it in GitHub Desktop.
waitfor()
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
//********************************************************************** | |
// function waitfor - Wait until a condition is met | |
// | |
// Used from: http://stackoverflow.com/a/14811679 | |
// | |
// Needed parameters: | |
// test: function that returns a value | |
// expectedValue: the value of the test function we are waiting for | |
// msec: delay between the calls to test | |
// callback: function to execute when the condition is met | |
// Parameters for debugging: | |
// count: used to count the loops | |
// source: a string to specify an ID, a message, etc | |
//********************************************************************** | |
function waitfor(test, expectedValue, msec, count, source, callback) { | |
// Check if condition met. If not, re-check later (msec). | |
while (test() !== expectedValue) { | |
count++; | |
setTimeout(function() { | |
waitfor(test, expectedValue, msec, count, source, callback); | |
}, msec); | |
return; | |
} | |
// Condition finally met. callback() can be executed. | |
console.log(source + ': ' + test() + ', expected: ' + expectedValue + ', ' + count + ' loops.'); | |
callback(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment