I create a setInterval method like below, I want to stop it anytime from anywhere. By the way, that function is in a if-else statement, so I cannot access the stop function.
How can it be possible?
var intervalID = setInterval(function () {
if (flag) {
stopTimer();
Pages.Page1.Label1.text = "";
}
if (time == 0) {
stopTimer();
Pages.Page1.Label1.text = "";
Device.makeCall(callNumber.rows[0][0]);
} else
updateTime();
}, 1000);
function updateTime() {
time = time - 1;
Pages.Page1.Label1.text = time;
}
function stopTimer() {
clearInterval(intervalID);
}
Just set the flag variable to
true
.Alternatively, i would suggest to declare your functions outside of the if / else scope .
For instance, create a timer instance with start / stop methods as such :
This way you can handle your timer as you want.