Skip to content

Instantly share code, notes, and snippets.

@stugoo
Created February 20, 2013 11:10
Show Gist options
  • Save stugoo/4994847 to your computer and use it in GitHub Desktop.
Save stugoo/4994847 to your computer and use it in GitHub Desktop.
Raw JS Ajax handler with response time in success
fireAJAXRequest = function(method, url, async, callback) {
var httpRequest,
start= new Date().getTime(),
end;
if (window.XMLHttpRequest) {
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) {
// Internet Explorer is stupid
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
end = new Date().getTime()-start;
httpRequest.responseTime = end;
callback(httpRequest);
}
// TODO write additional response handlers
};
httpRequest.open(method, url, async);
httpRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
httpRequest.send();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment