Request Queue
by grippstick
HTML
<script src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<textarea id='txt' style="width:100%;height:500px;"></textarea>
JavaScript
var txt = document.querySelector('#txt');
var writeLog = function(msg) {
if (txt.value) {
txt.value = txt.value + "\n" + msg;
} else {
txt.value = msg;
}
};
var PromiseQueue = function() {
var _PromiseQueue = new $.Deferred().resolve().promise();
var getResource = function(url) {
var def = new $.Deferred();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
writeLog(xhttp.readyState);
if (xhttp.readyState == 4 && xhttp.status == 200) {
// Action to be performed when the document is read;
def.resolve(xhttp.responseText);
} else if (xhttp.readyState == 4 && xhttp.status !== 200) {
def.reject(xhttp.responseText);
}
};
xhttp.open("GET", url, true);
xhttp.send();
return def.promise();
}
PromiseQueue.prototype.queueResults = function(url, func) {
var promise = getResource(url);
_PromiseQueue = _PromiseQueue.then(function() {
return promise;
});
promise.done(func);
return _PromiseQueue;
}
}
var pq = new PromiseQueue();
pq.queueResults("https://aligntoday.com/mobile/dashboardmobile.aspx",function(){writeLog});
pq.queueResults("https://aligntoday.com/mobile/dashboardmobile.aspx",writeLog);
pq.queueResults("https://aligntoday.com/mobile/dashboardmobile.aspx",writeLog);
pq.queueResults("https://aligntoday.com/mobile/dashboardmobile.aspx",writeLog);
pq.queueResults("https://aligntoday.com/mobile/dashboardmobile.aspx",writeLog);