Skip to content

Instantly share code, notes, and snippets.

@sjonesyodle
Created September 19, 2013 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjonesyodle/6627868 to your computer and use it in GitHub Desktop.
Save sjonesyodle/6627868 to your computer and use it in GitHub Desktop.
/*
Options :
- prop //property being evaluated
- context //context of `prop` so it can be referenced
- test //value `prop` is being evaluated for
- tries //max number of tries for test
- interval //frequency of tries in ms
- successCB //success callback
- failureCB //failure callback
*/
var testing = false;
function waitFor ( O ) {
var
prop = O.prop,
context = O.context,
test = O.test,
tries = O.tries || 20,
interval = O.interval || 100,
successCB = O.successCB || function (){},
failureCB = O.failureCB || function (){},
hn, _tries = 0, check;
if ( !prop || !context || !( prop in context ) ) return;
check = function () {
return context[prop] === test;
};
if ( check() ) {
successCB();
return;
}
hn = setInterval(function(){
if ( check() ) {
clearInterval(hn);
successCB();
return;
}
if ( !(++_tries < tries) ) {
clearInterval(hn);
failureCB();
}
}, interval);
};
waitFor({
prop : "testing",
context : window,
test : true,
tries : 10,
interval : 1000,
successCB : function () {
console.log("success");
},
failureCB : function () {
console.log("failure");
}
});
//testing = true;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment