Skip to content

Instantly share code, notes, and snippets.

@octopuss
Last active September 21, 2015 09:14
Show Gist options
  • Save octopuss/b60f95e24e8861e108bc to your computer and use it in GitHub Desktop.
Save octopuss/b60f95e24e8861e108bc to your computer and use it in GitHub Desktop.
Get status of some process periodically or timeout
var set_delay = 1000;
var ticker = 0;
var limit = 10;
var status = false;
callout = function (callback) {
$.ajax({
url: "http://private-anon-ca0f912b9-mcrdataforpublic.apiary-mock.com/"
})
.done(function (response) {
console.log("requested");
status = Math.random()>0.9; // in production should be somehow computed from response
if(status) {
callback(status);
}
})
.always(function (stg) {
ticker++;
if (ticker < limit && !status) {
console.log("status tested " + ticker);
setTimeout(callout(callback), set_delay);
} else if (ticker >= limit) {
console.log("status expired " + ticker);
callback(false);
}
});
};
// initial call
callout(cb);
function cb(status) {
console.log("finished " + status);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment