Created
February 20, 2013 11:10
-
-
Save stugoo/4994847 to your computer and use it in GitHub Desktop.
Raw JS Ajax handler with response time in success
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
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