Javascript:当我多次按下警报时,询问如何完全取消警报,我应该取消该函数的所有调用
我有一个一次取消1个警报的代码示例:Javascript:当我多次按下警报时,询问如何完全取消警报,我应该取消该函数的所有调用,javascript,Javascript,我有一个一次取消1个警报的代码示例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-`scale=1.0">` <meta http-equiv="X-UA-Compat
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-`scale=1.0">`
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>Click the first button alert "Hello" after 3 second</p>
<p>Click the second button to cancel the alert</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myStopFunction()">Stop the Alert</button>
<script>
var myVar;
function myFunction(){
myVar=setTimeout(function(){alert("Hello")},3000);
}
function myStopFunction(){
clearTimeout(myVar);
}
</script>
</body>
</html>
`
文件
在3秒钟后单击第一个按钮警报“Hello”
单击第二个按钮取消警报
试试看
停止警报
var-myVar;
函数myFunction(){
myVar=setTimeout(函数(){alert(“Hello”)},3000);
}
函数myStopFunction(){
清除超时(myVar);
}
但我想取消所有的警报,如果你按下试试它,例如
如3或4次,它将有4或3个警报,但如果您按下按钮,请尝试
和警报3次和3次在按钮上停止我的警报警报警报将
取消。
因此,点击次数将相等。我想创建一个函数,如果你按下它百万次
然后按1次停止警报,无论发生什么,警报都会取消。
我尝试实现此代码,但没有成功,因此我需要帮助
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-`scale=1.0">`
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p>Click the first button alert "Hello" after 3 second</p>
<p>Click the second button to cancel the alert</p>
<button onclick="myFunction()">Try it</button>
<button onclick="myStopFunction()">Stop the Alert</button>
<script>
var myVar;
var c`enter code here`ounter=0;
function myFunction(){
myVar=setTimeout(function(){alert("HEllo")},3000);
counter++;
}
function myStopFunction(){
for(var i =0;i<counter+1;i++){
clearTimeout(myVar);
counter--;
}
}
</script>
</body>
</html>
`
文件
在3秒钟后单击第一个按钮警报“Hello”
单击第二个按钮取消警报
试试看
停止警报
var-myVar;
var c`在此处输入代码`counter=0;
函数myFunction(){
myVar=setTimeout(函数(){alert(“HEllo”)},3000);
计数器++;
}
函数myStopFunction(){
对于(var i=0;i实际上,您的代码只删除了最后一个timeoutID
您可以将您的timeoutID放入数组。在您可以将所有timeoutID清除到循环中之后
var arrTimeout=[];
函数myFunction(){
var myVar=setTimeout(函数(){
console.log(“你好”);
}, 3000);
arrTimeout.push(myVar);
}
函数myStopFunction(){
arrTimeout.forEach((timeoutID)=>{
clearTimeout(timeoutID);
})
arrTimeout=[];
}
试试看
停止警报
是否要设置超时负载?为什么不在重置超时之前清除超时,那么您只有一个超时br您的代码不起作用,我会问一个特定的问题,这是使用java脚本中的警报命令,而不是使用页面上打印的console.log,因为警报是由数字执行的点击次数你点击试试看。谢谢我需要帮助请!任何人!首先我不是你的兄弟…我的代码工作。如果你想要警报,请更改console.log by alert。你使用的是Javascript而不是java scriptOK。但是我试过你的代码并被alert命令替换,仍然不工作。我感谢你的帮助人员,但请再试一次n使用警报。我的代码工作,你能描述一下故障,以及使用的浏览器。