cvcvka5
cvcvka5

Reputation: 57

How to reach a variable that is in a function from another function?

How can I reach the variable "count_down", from the other function called "STOP" so I can "clearInterval(count_down)".

Code

    function START(){
        let count_down = setInterval(countDown, 1000);
        up.setAttribute("disabled" , true)
        down.setAttribute("disabled" , true)
        count.innerHTML = "stop";
        return count_down;
    }


    function STOP(){
        clearInterval(count_down)
        up.removeAttribute("disabled");
        down.removeAttribute("disabled");
        count.innerHTML = "start";
    }

Troubleshoot:

Uncaught ReferenceError: count_down is not defined
    at STOP (main.js:112)
    at HTMLButtonElement.count.onclick (main.js:123)

Upvotes: 0

Views: 24

Answers (1)

Harmandeep Singh Kalsi
Harmandeep Singh Kalsi

Reputation: 3345

You can do something like this.

let count_down=0;

function START(){
        count_down = setInterval(countDown, 1000);
        up.setAttribute("disabled" , true)
        down.setAttribute("disabled" , true)
        count.innerHTML = "stop";
        return count_down;
    }


    function STOP(){
        clearInterval(count_down)
        up.removeAttribute("disabled");
        down.removeAttribute("disabled");
        count.innerHTML = "start";
    }

Upvotes: 1

Related Questions