Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top