在 javascript 中,当网页未最小化或打开新选项卡时,清除超时前的计时器
Clear a timer before timeout in javascript when the webpage is not minimized or new tab is opened
我在JavaScript中有一个递归类型函数onload,它像这样运行:我已将弹出窗口的超时设置为 5 秒,这工作正常。但是我想清除计时器并在用户移动到新选项卡时关闭弹出窗口,所以我在 JavaScript 中使用了"document.hidden"属性但是当我运行脚本并移动到新选项卡或最小化浏览器中的窗口时,计时器仍未清除,超时继续 5 秒我错过了什么.请指教
function callMe()
{
var delay ;
$("#dialog").dialog({
modal: true,
title: "Confirmation Window1",
resizable: false,
width: 300,
height: 150,
open: function (event, ui) {
delay = setTimeout(function () {$("#dialog").dialog("close"); setTimeout(callMe,1000); }, 5000);
if(document.hidden)
{
clearTimeout (delay);
$(this).dialog("close");
setTimeout(callMe,1000)
}
},
buttons: {
Ok: function () {
$(this).dialog("close"); //closing on Ok
clearTimeout (delay);
setTimeout(callMe,1000)
},
Cancel: function () {
$(this).dialog("close"); //closing on Cancel
clearTimeout (delay);
setTimeout(callMe,1000)
}
}
});
我能够通过使用window.blur和window.focus来解决这个问题。代码如下:
var delay ;
function callMe()
{
$("#dialog").dialog({
modal: true,
title: "Confirmation Window1",
resizable: false,
width: 300,
height: 150,
open: function (event, ui) {
delay = setTimeout(function () {$("#dialog").dialog("close"); setTimeout(callMe,1000); }, 2000);
},
buttons: {
Ok: function () {
$(this).dialog("close"); //closing on Ok
clearTimeout (delay);
setTimeout(callMe,1000);
},
Cancel: function () {
$(this).dialog("close"); //closing on Cancel
clearTimeout (delay);
setTimeout(callMe,1000);
}
}
});
}
$(window).blur(function(e) {
clearTimeout (delay);
$("#dialog").dialog("close");
});
$(window).focus(function(e) {
setTimeout(callMe,1000);
});
相关文章:
- 我的单元测试选项是什么
- 如何更改bigquery API中的计费层选项
- 使用Javascript获取所选选项ID
- Selectize.js:如何对整数值的选项进行排序
- 通过js在新选项卡中有条件地打开url
- 按照选项卡索引的顺序循环一个jQuery选择
- 如何在使用selectBoxIt JQuery插件时检测选项更改
- 无法在Ionic select中预先选择最后一个选项
- 如何在选项卡上定义属性'的主窗口对象
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 使用此选项选择父类内部的类
- CKeditor:更改对话框中的默认选择选项
- JQuery覆盖不更改单选选项
- 活动选项卡's源代码-获取变量s值
- 绑定时将Parsley minlength消息作为选项传递时,未对其进行自定义
- 如何从选择框中的选项中获取属性值
- 如何在从多选下拉列表中选择选项值时动态生成文本框
- 在 javascript 中,当网页未最小化或打开新选项卡时,清除超时前的计时器
- Javascript 会话超时,多个选项卡出现弹出警报
- 使用超时循环将数据加载到iframe的更好选项