Last active
September 21, 2015 09:14
-
-
Save octopuss/b60f95e24e8861e108bc to your computer and use it in GitHub Desktop.
Get status of some process periodically or timeout
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
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