0

Why is the native setInterval function adding 1 milliseconds after ~each new function call, which will be triggered by the interval?

This the log I get after I pinged the API for at least 20 times:

2017-09-10T12:42:56.568Z INFO: Pinging API
2017-09-10T12:42:58.569Z INFO: Pinging API
2017-09-10T12:43:00.569Z INFO: Pinging API
2017-09-10T12:43:02.570Z INFO: Pinging API
2017-09-10T12:43:04.570Z INFO: Pinging API
2017-09-10T12:43:06.571Z INFO: Pinging API
2017-09-10T12:43:08.571Z INFO: Pinging API
2017-09-10T12:43:10.571Z INFO: Pinging API
2017-09-10T12:43:12.572Z INFO: Pinging API
2017-09-10T12:43:14.572Z INFO: Pinging API
2017-09-10T12:43:16.572Z INFO: Pinging API
2017-09-10T12:43:18.573Z INFO: Pinging API
2017-09-10T12:43:20.574Z INFO: Pinging API
2017-09-10T12:43:22.574Z INFO: Pinging API
2017-09-10T12:43:24.575Z INFO: Pinging API
2017-09-10T12:43:26.576Z INFO: Pinging API
2017-09-10T12:43:28.576Z INFO: Pinging API
2017-09-10T12:43:30.576Z INFO: Pinging API
2017-09-10T12:43:32.577Z INFO: Pinging API
2017-09-10T12:43:34.577Z INFO: Pinging API

Currently my code is just doing the following inside the callback function:

setInterval(() => {
    // this just logs the info
    logger.info('Pinging API') 
}, 2000)

I hope some one can explain me, when the interval is reset and why it isn't reliable on a millisecond basis (never mind the nanosecond basis)?

5
  • 1
    developer.mozilla.org/en-US/docs/Web/API/… Commented Sep 10, 2017 at 12:55
  • 1
    setInterval is not accurate. JS code cant be time accurate. No code running on a thread based system can be time accurate. Commented Sep 10, 2017 at 12:56
  • In modern browsers ... but this is about NodeJS, which isn't a browser. Commented Sep 10, 2017 at 12:56
  • @kyon google V8 ... Commented Sep 10, 2017 at 12:57
  • Anyway, I'm just curious why it can't be? I would love to see an answer which explains this in a bit more detailed way. Commented Sep 10, 2017 at 13:00

2 Answers 2

2

Some self testing:

Take a piece of paper and write all the squares onto it, e.g. 1,4,9,16... , then take a clock and every 10 seconds knock on the table.

When doing the first numbers its easy and you can multithread (not multitask) easily. However when the math gets more complicated, you will forget about knocking and youre busy with calculating.

How is that related to processors?

A computer is running a few (thousand) jobs at the same time. It displays the time, changes your background image, loads updates, cleans up RAM, etc. To do this, it does multithreading, so it changes between jobs (threads) a few times per second. So it looks like that it does it the same time.

What happens with timers?

A computer has hardwareside timers. When a timer is finished, it interrupts the current proccess ( if possible!) and continues with handling the timer ( logging some stuff).

If possible?

Interrupting a process needs time ( switching RAM pages, cleaning up the register) and like a human can be busy with computations a computer can be too. Also interrupting too often can cause a livelock. Thats why timers are not precise as the computer awaits currently sheduled jobs until it does a context change. And to be honest: who cares about that millisecond?

Sign up to request clarification or add additional context in comments.

1 Comment

Do you also have a link to a certain post/blog, where I can read more about it? thank you for answering anyway, very helpful!
0

setInterval and setTimeout do not guarantee to execute the callback on exact time, you can read its api document in this page setTimeout#Reasons_for_delays_longer_than_specified

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.