There is no guarantee that interval will run every 1ms, it depends on what else happens in loop, JS will only try to run it required amount of times when it comes to running it (I mean that if there was no time to run this function for one second JS will try to run it 1000 times after this time). Also I think there is minimal interval that will be forced and its more than one millisecond.
Javascript setInterval if statement not working correctly
-
11-10-2022 - |
Frage
I've got a question:
if I use following code the if
statement will never be executed:
var timer = setInterval(function(){
// the date of new date is ofcourse a date in the future
if (Date.now() == Date.parse(new Date(2014, 1, 13, 13, 4, 0, 0))){
console.log('refresh done');
clearInterval(timer);
}
}, 1);
But if I change the ==
to >=
it will execute correctly. Is this because the Date.now()
will change so fast that it is changed before the comparison did took place?
Lösung
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow