Skip to content

Instantly share code, notes, and snippets.

@podgorniy
Created April 18, 2012 12:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podgorniy/2413131 to your computer and use it in GitHub Desktop.
Save podgorniy/2413131 to your computer and use it in GitHub Desktop.
Some condition waiter with no blocking ui with synchronious first condition call.
/*
example of use: wait for jQuery load for 5 sec
wait_for(function (){
return window.jQuery;
}, function () {
$('body').addClass('js');
}, 5000);
*/
function wait_for (condition, callback, timeout, interval) {
var interval = interval || 50,
waiter_interval;
(function waiter () {
var condition_result;
try {
condition_result = condition();
} catch (err) {
// error handling
}
if (condition_result) {
callback();
} else {
waiter_interval = setTimeout(waiter, interval);
}
}());
// stop waiting
if (timeout) {
setTimeout(function () {
clearInterval(waiter_interval);
}, timeout);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment