setInterval not working

I wanted to make a simple clock but for some reason, setInterval() is not working, can someone please check what’s wrong with it?

setInterval is working. If you stick a console.log statement inside the timer function you will see that it is called every second. I would concentrate on where you are updating the time variable, or should I say, where you are NOT updating the time variable.

Yes, you seem to be assuming that the new Date() is creating a time object that updates as time goes on. No, it takes a snapshot.

put let time = new Date() inside the timer function

We were trying to hint at the right answer instead of blurting it out. This is a teaching site.

setInterval(() => {
let time = new Date().toLocaleTimeString();
document.querySelector(‘h1’).innerHTML = time;
}, 1000);

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.