Skip to content

Instantly share code, notes, and snippets.

@lucamug
Last active October 3, 2016 19:59
Show Gist options
  • Save lucamug/1050b5dea0add87636d62524cdec9a7d to your computer and use it in GitHub Desktop.
Save lucamug/1050b5dea0add87636d62524cdec9a7d to your computer and use it in GitHub Desktop.
凸.waitForSomething
var 凸 = (function(凸) {
"use strict";
var id = 0,
waitForSomething = function (obj) {
obj = obj || {};
obj.c = obj.c || 1;
obj.cMax = obj.cMax || 10;
obj.verbose = obj.verbose || false;
obj.id = obj.id === undefined ? id++ : obj.id;
var log = function(text, text2) {
text2 = text2 || (obj.c === 1 ? 'START' : ' ');
if (obj.verbose)
console.log(text2 + 'id:' + obj.id + ' #' + obj.c + ' [' + obj.name + '] ' + text + ' [' + obj.waitFor(obj) + ']');
};
if (obj.c > obj.cMax) {
log('giving up', 'END');
} else {
if( obj.waitFor(obj) ) {
log('will execute thenDo', 'END');
return obj.thenDo( obj );
}
else {
var ms = Math.pow( 2, obj.c ) * 2 + 50;
log('will try again in ' + ms + 'ms');
obj.c++;
setTimeout(function () {
waitForSomething(obj);
}, ms);
}
}
};
凸.waitForSomething = waitForSomething;
return 凸;
}(凸 || {}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment