Created
September 19, 2013 18:34
-
-
Save sjonesyodle/6627868 to your computer and use it in GitHub Desktop.
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
/* | |
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