I have created an infinite loop to check if a certain time is met and change my interface accordingly.
function infiniteloop(){
let now = moment();
start = moment(myStartTime);
end = moment(myEndTime);
if (now.isBetween(start, end)) {
console.log('This is happening now')
} else {
console.log('This is not happening now')
}
setTimeout(infiniteloop, 5000);
}
infiniteloop()
is called when my page renders. As the variables myStartTims
and myEndTime
may change, it is necessary to call that function again when they change. But then it runs multiple times.
So I tried to call it with
clearTimeout(infiniteloop);
infiniteloop();
But that doesn't do the Trick. How can I ensure the loop is stopped and called again?