Last active
December 10, 2015 21:59
-
-
Save kjb/4498705 to your computer and use it in GitHub Desktop.
Javascript method that executes a test periodically until true, then triggers an action with the result of the test.
This file contains 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
var when = function( test, execute, delay ) { | |
delay = delay || 200; | |
var x = test(); | |
if ( x ) { | |
execute( x ); | |
} else { | |
setTimeout( function() { | |
when( test, execute, delay ); | |
}, delay ); | |
} | |
} | |
when( | |
function() { return confirm('Stop?');}, | |
function( x ) { alert('finally: ' + x); } | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment