1

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?

Dan
  • 2,304
  • 6
  • 42
  • 69

3 Answers3

3

From the docs:

delay

The time, in milliseconds (thousandths of a second), the timer should delay in between executions of the specified function or code. If this parameter is less than 10, a value of 10 is used. Note that the actual delay may be longer;

(Mozilla, but other browsers seem to use similar values)

There is also a reference to the documentation of setTimeout mentioning reasons why "the actual delay may be longer".

In short:

  • There is a minimum delay (already in the spec) for nested timeouts
  • Inactive tabs may clamp timeouts to reduce load
  • The page/browser/OS might be busy with other tasks

In modern browsers there seems to be a way using window.postMessage.

Marvin
  • 13,325
  • 3
  • 51
  • 57
  • Thank for explaining the delay in setTimeout/setInterval. I think comparing timestamps is a good way to go. – Dan Nov 22 '16 at 23:31
0

The delay for setInterval() and setTimeout() can be longer than the specified time. There are various reasons for this:

See MDN WindowTimers.setTimeout(): Reasons for delays longer than specified for more information.

Josa
  • 716
  • 6
  • 10
-2

if you multiplied the result by 4 it will get the real number in ms . i tried it just in chrome .

RustedMind
  • 43
  • 4