javascript – Is there any more reliable function than setInterval?


I have understood (and verified) that setInterval is not able to occur at exactly the set time, is there any other function that is capable of doing it? If not, how could I do an ajax to update the function?


In the browser you can userequestAnimationFrame , this function is executed up to a maximum of 60 times per second.

Inside this function you can "query" the time in milliseconds with new Date() or , and compare it to a timestamp of your choosing.

When the time in milliseconds is greater than your timestamp then you will execute the code that corresponds to that timestamp.

with this you would have a margin of error of 1000/60 = 16.6 milliseconds, which in 99% of cases is enough.

Scroll to Top