Skip to content

Instantly share code, notes, and snippets.

@kjb
Last active December 10, 2015 21:59
Show Gist options
  • Save kjb/4498705 to your computer and use it in GitHub Desktop.
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.
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