+ 4

Can I use decimal milliseconds in setInterval delay?

I want to use decimal milliseconds in setInterval.The problem is I don't know if it's possible or not. Even It's hard for me to test in decimal millisecond delay because I can't see even if it works or not. Does using setInterval convert the time to nearest int milliseconds or the decimal milliseconds really work? Thanks in advance.

26th Jun 2021, 12:33 PM
The future is now thanks to science
The future is now thanks to science - avatar
2 Answers
+ 1
Hello The future is now thanks to science[LESS ACTIVE] , If you want to check that the decimal milliseconds work or not you can check the code give below. In the code you have to specify the interval in the variable named as "speed". I have given default value of speed as 1000, var speed = 1000 It will show you that how many times the function has been called in 10s if it was called in the interval of 1000 (which is the value of speed) So the answer will be 10000/1000 = 10 This shows that the function was called 10 times in 10s if the interval was 1000. But if you will give speed = 10 This will give you result 995(its is showing in my phone) But it should have given result 1000 but it is 995. Similarly you can try for 1 or 0.1 etc. For 1 ms it is giving result close to 2300 but it should have given 10000, I don't know if it is happening with my phone only. https://code.sololearn.com/Wucfbly3wlgx/?ref=app
26th Jun 2021, 6:33 PM
Meetesh - avatar
+ 1
setInterval (and setTimeout) function(s) are not accurate for the delay used between each calls: the delay do not take into account the function time execution, nor other less or more blocking tasks (javascript is mono-threaded)... also there's a minimal amount delay: 0 do not run the callback function immediatly, nor as soon as the current process is done... so, in this context, knowing if we "can use decimal milliseconds delay" is pointless, as the interval would never be as much accurate to take even one millisecond of precision ^^ if you want to get a few more precision in your delay, you could use requestAnimationFrame wich run callback just before a display refresh by providing a high res timestamp (decimal milliseconds) as argument... it should be called less frequently than setInterval callback, but only when displayed information are ready to be shown, and avoiding to measure yourself the time ellapsed between two calls (well, you must store the last timestamp and do substraction to get the delay)
26th Jun 2021, 6:55 PM
visph - avatar