Sometimes, I found that setInterval
will be inaccurate and unreliable.
For example,
let time = document.querySelector("div");
setInterval(function () {
console.log(window.getComputedStyle(time).getPropertyValue("opacity"));
time.textContent = parseFloat(time.textContent) + 1;
}, 1);
<div>0</div>
The timer is much slower than 1 ms which means the setInterval
doesn't work properly.
Why does this happens?
What is a much better and reliable alternative option for setInterval
?
Thanks for any responds!