I'm wondering how to stop this function when next is not equal to targetVar and here's my code :
function detectChange(next,targetVar){
if (next==targetVar){
waktuSholat();
console.log("Waktu Sholat Berhasil Di Perbarui");
} else if (next!=targetVar){
speakIt();
}
}
setInterval(detectChange, 2000);
I don't know how to stop this interval. I really appreciate your help and your suggestion.
Store the id returned by setInterval and call clearInterval to stop it. Note that detectChange is called each time without any arguments, so both next and targetVar are always undefined; perhaps you meant to check global variables instead.
function detectChange(next,targetVar){
if (next==targetVar){
waktuSholat();
console.log("Waktu Sholat Berhasil Di Perbarui");
} else {
speakIt();
clearInterval(id);
}
}
let id = setInterval(detectChange, 2000);