Function.prototype.promise = function () {
/* Arguments:
condition to be met before running
polling interval
arguments for the function
*/
var __method=this,
oArgs=arguments,
args=[],i,__cond=oArgs[0],__intv=oArgs[1];
for (i=2; i<oArgs.length; i++) args.push(oArgs[i]);
if (__cond.apply(__cond,args))
this.apply(this,args);
else window.setTimeout(function () {
__method.promise.apply(__method,oArgs);
},__intv*1000);
}
/* Very simplistic example of use */
function fnc(a) { //What we're promising to do
alert(a)
}
//Dummy variable that's just not what we need it to be... yet
var valueToTrack=5;
//Make the promise
fnc.promise(
function() {
return valueToTrack==4;
},1,'I\'ve kept my promise'
);
// at some point in the future, the condition changes...
setTimeout("valueToTrack--;", 4000);