Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to end setinterval?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading