JSFiddle - React, Tailwind, and code Playground
by dexygen
JavaScript
(function(root) {
var instance,
countdown = function() {};
root.XhrCountdown = function() {
instance = instance || (function() {
return {
requestDone: function() {
countdown();
},
sendAll: function(requests) {
var totalRequests = requests.commands.length;
countdown = function() {
if (!--totalRequests) {
requests.handlers.allSent();
}
};
for (var i=totalRequests; i--; ) {
requests.commands[i].send();
}
}
}
})();
return instance;
}
})(this);
var requests = {
commands: [{
send: function() {
new Request.JSON({
url: '/echo/json/',
data: { json: JSON.encode({
reqOneTime: (new Date()).getTime()
}), delay: 2 },
onSuccess: function(response) {
console.log(response);
XhrCountdown().requestDone();
}
}).send();
}
}, {
send: function() {
new Request.JSON({
url: '/echo/json/',
data: { json: JSON.encode({
reqTwoTime: (new Date()).getTime()
}), delay: 0 },
onSuccess: function(response) {
console.log(response);
XhrCountdown().requestDone();
}
}).send();
}
}],
handlers: {
allSent: function() {
console.log('allSent');
}
}
}
XhrCountdown().sendAll(requests);