I'm trying to time how long a file takes to download using an HTTPRequest like so:
function getFile() {
'use strict';
var url = "data.bin";
var rawFile = new XMLHttpRequest();
var timer_var = setInterval( theTimer, 1 );
rawFile.open("GET", url, true);
rawFile.onreadystatechange = function () {
if(rawFile.readyState === XMLHttpRequest.DONE && rawFile.status === 200) {
toLog(rawFile.responseText);
window.clearInterval(timer_var);
toLog("Milliseconds for download: " + time_taken);
}
};
rawFile.send(null);
}
function theTimer() {
'use strict';
toLog(time_taken);
time_taken++;
}
As you can see I have setInterval
calling theTimer
every one millisecond. All thetimer()
does is increment a variable, which in theory should have a value in milliseconds of how long the interval was running.
When the file has been downloaded I output the data, clear the timer and display the time in ms. However, the value doesn't add up. I should be almost 2 seconds but only stands at around 250ms.
Why isn't the setInterval
truly every 1ms?