0

The second parameter in the setInterval function is for delaying the executing in milliseconds. and as expected 1000 millisecond will trigger the function every second. but here's a strange thing that I don't really understant. In the second interval, I've set the delay parameter to 1 millisecond and put inside the function incremental number and condition to check for every passed 1000 number.. I was expecting it to behave just like the seconds interval timer. but It doesn't quite run as I hoped.

Is there any explanation for that?

// detect seconds in a second interval timer (works as expected)
setInterval(function() {
    console.log('passed second from the secondInterval');
}, 1e3);

// detect seconds in a millisecond interval timer (I don't have any explanation for the behavior).
(function() {
    var i = 0;
    setInterval(function() {
        if (i === 0) {
            i++;
            return;
        }

        if (i % 1000 === 0) console.log('passed second from the millisecondInterval');
        i++;
    }, 1);
})();

check out the example in jsfiddle to see what I mean http://jsfiddle.net/dwh82zos/

Husamuddin
  • 405
  • 1
  • 7
  • 19
  • http://stackoverflow.com/questions/9647215/what-is-minimum-millisecond-value-of-settimeout – CD.. Sep 03 '15 at 19:16
  • 1
    The delay is expressed in milliseconds, but does not have millisecond accuracy. Think of it more as a suggestion. Do you really want webpages running code 1000 times per second? The real minimum is probably on the order of ~10-15ms, depending on the browser, OS, etc. – Cameron Sep 03 '15 at 19:16
  • @Cameron that will be really bad if you are working on animation frames and you really need it to be so accurate. – Husamuddin Sep 03 '15 at 19:22
  • @symcbean.. I think it's quite clear. I need to compare the detection for seconds in a millisecond timer and a second timer.. I'm asking about why it doesn't work as expected! what is the thing that you don't understand?! – Husamuddin Sep 03 '15 at 19:33
  • 1
    @Husamuddin: You can [measure time](http://stackoverflow.com/a/10767937/21475) accurately enough, you just can't have your callback invoked at exact intervals. Native games have exactly the same problem -- the Win32 sleep resolution, for example, is only to within 10-15ms, and there's always jitter anyway depending on what else is running on the system, etc. Look into [`requestAnimationFrame`](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame) -- the browser manages the callback interval for you. – Cameron Sep 03 '15 at 19:53
  • Thanks @Cameron for your help, I'll check that article. It's really annoying that I got minus for my question. I really don't understand what's wrong with the question!.. anyway. thanks for help. – Husamuddin Sep 03 '15 at 20:03

1 Answers1

1

This happens. The sentence is not logged every second, or whatever your machine can. You are running out of memory, can't resolve, first time 6 operations per 1ms and second 3 operations per 1ms. Take a look of this What is minimum millisecond value of setTimeout? it is about setTimeout but it useful.

Community
  • 1
  • 1
Miguel Lattuada
  • 5,327
  • 4
  • 31
  • 44