I have problem is interval. When i declare function she have setinterval and if i clearinterval she continues to be executed. Code:
if (score == 1) {
leftBlinkTimer(0)
} else if (score == 0) {
leftBlinkTimer(1)
}
function leftBlinkTimer (state) {
let leftBlin = 0;
var timer;
if (state == 0) {
timer = setInterval(() => {
if (leftBlin == 0) {
const newData = {
stateLeftBlinker: 'rgba(255, 255, 255, 1)'
}
vm.changeMicro(newData)
leftBlin = 1;
} else if (leftBlin == 1) {
const newData = {
stateLeftBlinker: '#47EC5B'
}
vm.changeMicro(newData)
leftBlin = 0;
}
},600)
} else if (state == 1 || state == '1') {
clearInterval(timer)
}
I try check state in timer and clear, and it’s not working. Both 0 and 1 come to state.Thanks for help 🙂
I’m waiting for the time to end
>Solution :
The issue you’re facing is related to the scoping of the timer variable inside the leftBlinkTimer function. The timer variable is declared inside the function’s scope, so when you call clearInterval(timer), it won’t refer to the same timer that was created in the previous call to leftBlinkTimer.
declaring it outside would help