I'm attempting to measure the duration of a file download using an HTTPRequest as seen below:
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++;
}
It is evident that I have utilized setInterval
to invoke theTimer
every millisecond. The function theTimer()
simply increments a variable, which theoretically should hold the time in milliseconds of how long the interval was executing.
Upon completion of the download, I present the data, stop the timer, and exhibit the time in milliseconds. Nonetheless, the recorded value does not align. It should be close to 2 seconds, yet it remains at around 250ms.
Why is the setInterval
not truly every 1ms?