gpt4 book ai didi

javascript - 如何停止 react 计时器?

转载 作者:行者123 更新时间:2023-12-03 14:29:47 25 4
gpt4 key购买 nike

我试图在显示通知开始时停止计时器,因为计时器不断添加。但是我在做这件事时遇到了困难。有什么想法吗?

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

感谢您的帮助!

最佳答案

尝试:

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)

关于javascript - 如何停止 react 计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60908691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com