简体   繁体   中英

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)

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"; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM