我在我的div类上使用的网站上有一个特定的甜蜜警报,该警报以onclick打开,但例如,我希望此onclick警报在60秒后停止。因此,在60秒后,如果我单击div,警报将不会显示。这就是我到目前为止所得到的
<script type="text/javascript">
function JSalert(){
swal("Error", "You have to watch the video first.", "error");
}
</script>
<div class="blocked" onclick="JSalert()">
答案 0 :(得分:4)
据我了解的问题(并基于警报中的翻译文本):
(在OP中:“如果在60秒钟后单击div,警报将不会显示”,或者换句话说,“如果在60秒后单击div,警报将不会显示”)>
var showalert = true;
setTimeout(function() { showalert = false; }, 5000); /* 5s for testing */
function JSalert(){
if (showalert) {
alert("Please don't click too soon");
}
}
<div class="blocked" onclick="JSalert()">click me, but not too soon</div>
可以使用data-
属性/ jquery .data()
对此进行改进,但是概念是相同的。
答案 1 :(得分:-1)
您要查找的是setTimeout:https://www.w3schools.com/jsref/met_win_settimeout.asp
在JSalert函数中类似的东西:
setTimeout(function() {
// close modal here
}, 60000);
或者检查甜蜜警报文档,还有一个计时器: https://sweetalert.js.org/docs/#timer