javascript - 停止计时器 JavaScript
问题描述
如果页面上显示 div 错误 div,我正在尝试停止计时器。这是代码。我不确定为什么 clearTimeOut 没有停止计时器。
<script type="text/javascript">
function timeOutRedirect(){
var delay = 60000; // time in milliseconds
// Show message div
document.getElementById("message").style.display = "block";
// Display message
document.getElementById("message").innerHTML = "<h1>Your order is being processed.</h1>";
setTimeout(function(){
if( document.getElementsByClassName('woocommerce-NoticeGroup-checkout').length != 0 ){
// If error div is loaded
delay = clearTimeout(); //this is n0t clearing timer
document.getElementById("message").style.display = "none";
console.log('error notice div loaded');
} else {
window.location = "/processing-information/";
}
},delay);
}
</script>
<!-- Time out double order timer -->
<div id="message" style="background: #a1f5b9; margin: 10px 0; padding: 10px; text-align: center; display: none; z-index: 99999;"></div>
解决方案
如果你有一个间隔,你可以使用 clearInterval() 方法。
const handle = setInterval(function(){...},delay);
...
// Cancel the interval
clearInterval(handle)
对于计时器,您可以使用 clearTimeout() 方法。
const handle = setTimeout(function(){...},delay);
...
// Cancel the timer
clearTimeout(handle)
推荐阅读
- node.js - 如何在没有互联网的情况下仅安装节点模块
- php - 按以字符串开头的子元素过滤数组
- redhat-datavirt - JDV 中的一些日期值按天计算
- python - SyntaxError:使用 .format 和 for 循环解析时出现意外 EOF
- webview - Xamarin.Forms.WebView 在访问 URL 之前将项目添加到本地存储
- jsonpath - 根据子键过滤 JSON 对象
- python - 如何在 Keras 中实现 1-sigmoid?
- c# - 检查进程是否有响应
- reactjs - @blueprintjs/table 如何使用箭头/tab 键导航
- scala - 无法从 Scala 脚本停止和 rm docker 容器