Or, the way real coders do it:
Or sane people.
A numerical identifier for the timeout request (used with clearTimeout() &c.).
Neither of these solutions will work, of course, because "bit" is local to the onchange function.
Code:
var Xhr = (function() {
var xhr = null;
function newXhr() {
if(window.XMLHttpRequest)
return new XMLHttpRequest();
else if(window.ActiveXObject)
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
function getXhr() {
return xhr || (xhr = newXhr());
}
function getFile(url, callback) {
var xhr = getXhr();
xhr.open("GET", url, !!callback);
if(callback)
xhr.onreadystatechange = function() {
if(this.readyState === 4)
return callback(this.responseText);
};
xhr.send(null);
return callback ? undefined : xhr.responseText;
}
return {
newXhr: newXhr,
getXhr: getXhr,
getFile: getFile
};
})();
// I'm not sure what this function should be called, but
// "getPage" is woefully inaccurate.
function awakenLateParrot(t) {
Xhr.getFile("http://webpage.com/page.php" + (t ? "?t=" + t : ""),
function(text) {
// Hurry up o ye days of destructuring assignment and
// (nextT, toAppend) = text.split("|"); // :-(
var nextT = text.split("|"),
toAppend = nextT[1];
nextT = nextT[0];
document.getElementById("text").appendChild(document.createTextNode(toAppend));
setTimeout(function() { awakenLateParrot(nextT); }, 1000);
});
}
Bookmarks