Created
April 18, 2012 12:03
-
-
Save podgorniy/2413131 to your computer and use it in GitHub Desktop.
Some condition waiter with no blocking ui with synchronious first condition call.
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
/* | |
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