简体   繁体   中英

How do I stop timer in react?

I am trying to stop the timer in the beginning of my display notification because timer keeps getting added. However I am having trouble doing it. Any ideas?

export function displayNotification(msg, bool){
    var d = document.getElementById("notification");
    d.innerHTML = msg;
    d.style.opacity = "1";

    if(bool){d.style.backgroundColor="#77DD77"}
    else{d.style.backgroundColor="rgb(196, 88, 68)"}
    window.setTimeout(function(){
        d.style.opacity = "0";
        d.innerHTML = "";
    },3000);
}

Thank you for all your help!

try :

export function displayNotification(msg, bool){
    var d = document.getElementById("notification");
    d.innerHTML = msg;
    d.style.opacity = "1";

    if(bool){d.style.backgroundColor="#77DD77"}
    else{d.style.backgroundColor="rgb(196, 88, 68)"}
    let timer = window.setTimeout(function(){
        d.style.opacity = "0";
        d.innerHTML = "";
    },3000);
    return timer
}

let timer = displayNotification(msg, bool)
clearTimeout(timer)

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