I have a function (called metro) that uses the current local time. I also have a setInterval that calls this function every 30 seconds, but the setInterval is returning always the same value, when my intention is that the time gets updated by the new Date()
let now = new Date()
let hourNow = now.getHours()
let minutesNow = now.getMinutes()
const interval = setInterval(metro, 30000, hourNow, minutesNow);
>Solution :
If you want the times to change you’ll need to create your date and the other variables inside the interval function.
const interval = setInterval(() => {
let now = new Date()
let hourNow = now.getHours()
let minutesNow = now.getMinutes()
metro(hourNow, minutesNow);
}, 30000);