I have written a loop, which I want to run, every second, until the present time is equal to a time specified outside of the loop. If the time matches, it would log 'it is time', and break out of the loop; otherwise, it would run again in one second.
I've used node to run the file, and the file hangs on the execution, so I have to CTRL+C out, but it never prints anything.
This is clearly a bug on my end, but I can't figure out why it's happening.
while (true) { async ()=>{
const now = new Date();
const nowHour = now.getHours();
const nowMinute = now.getMinutes();
if (nowHour === hTarget && nowMinute === mTarget) {
console.log('it is time!');
return; // exit the loop when the target time is reached
}
console.log('not yet');
// Wait for 1 second before checking again
// to avoid excessive CPU usage
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
I've tried running it without an async function, but I just run into stackoverlow errors.