清除后如何启动javascript setinterval?
How to start javascript setinterval once cleared?
我有一个每 5 秒运行一次的 setinterval。这在页面加载时工作正常。
我有以下几种情况:
- 按时间间隔加载页面 (WORKS)
- 按下按钮并加载新内容并停止间隔(WORKS)
- 一旦不再需要新内容,将其关闭,return 到第一个内容并再次开始间隔(不起作用)
- 我有诸如 window.blur 事件之类的安全性,它也会停止间隔,以便浏览器不会对所有丢失的间隔进行补偿,如果我要更改选项卡或其他东西。请记住,第 3 步不起作用 但是 如果我在第 3 步之后更改选项卡然后 return 到我的原始页面(执行模糊)间隔将再次开始工作.
注意 除页面加载外,此处加载的所有内容都是通过 ajax 调用完成的。
我的代码:
正在初始化:
$.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function() {
$.automation.tanks.tableInit();
});
绑定函数:
bindIntervalEvent: function (target, url, callback) {
$(window)
.on("focus.mine",
function() {
$.automation.worker.setUpdateInterval(target, url, callback);
})
.on("blur",
function() {
$.automation.worker.stopUpdateInterval();
}).trigger("focus.mine");
}
区间函数:
setUpdateInterval: function (target, url, callback) {
if ($.automation.globals.globalInterval.value.length === 0) {
$.automation.globals.globalInterval.value.push(window.setInterval(
function () {
var options = {
loadTarget: target
}
$.automation.worker.getView(url,
function() {
if (callback)
callback();
},
options);
},
5000));
}
}
停止区间的函数:
stopUpdateInterval: function () {
if ($.automation.globals.globalInterval.value.length === 0)
return;
console.log("deleting");
for (var i = 0; i <= $.automation.globals.globalInterval.value.length; i++) {
window.clearInterval($.automation.globals.globalInterval.value[i])
$.automation.globals.globalInterval.value.splice(i, 1);
console.log($.automation.globals.globalInterval.value.length);
}
}
停止间隔时,我还删除了 window 绑定:
unBindIntervalEvent: function() {
$(window).off("focus.mine");
$(window).unbind("blur");
}
返回第 3 步:
我的 getviewfunction 回调中的成功方法与我一开始执行的相同
代码:
$(".updatelatest")
.on("click",
function () {
var _this = $(this);
var options = {
loadTarget:"#TanksContent"
}
$.automation.worker.getView("/Tank/GetTanks",
function (data) {
$(_this).switchClass("col-md-5", "col-md-1", 1000, function() {
$(_this).addClass("hidden");
$(".search").switchClass("col-md-5", "col-md-12", 1000, "easeInOutQuad");
})
$.automation.tanks.tableInit();
$.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function () {
$.automation.tanks.tableInit();
});
$(window).trigger("blur");
}, options);
});
但这不会开始间隔。它显然已初始化,因为它在执行 window.blur 时有效,例如当我更改选项卡时,但由于某种原因,它无法正常工作。
我尝试触发 windows 模糊事件但没有任何反应,我尝试触发我的自定义 window 事件 "focuse.mine" 但没有任何反应。
我在开发时没有注意到这一点,因为我打开了 firebug,每次我检查脚本或 css 或控制台时,都会执行模糊功能,所以我认为我的代码按预期工作但现在它已部署,我注意到了这一点。
我的脑袋莫名其妙地怦怦直跳,我想不出我哪里做错了。
嗯,这很有趣。我只是发现在调用 setUpdateInterval() 时;直接运行它给了我想要的结果。
我意识到我让它们像我那样分开的原因是因为模糊事件。 "Focus.mine" 被触发以在用户返回页面时再次开始间隔。
我有一个每 5 秒运行一次的 setinterval。这在页面加载时工作正常。
我有以下几种情况:
- 按时间间隔加载页面 (WORKS)
- 按下按钮并加载新内容并停止间隔(WORKS)
- 一旦不再需要新内容,将其关闭,return 到第一个内容并再次开始间隔(不起作用)
- 我有诸如 window.blur 事件之类的安全性,它也会停止间隔,以便浏览器不会对所有丢失的间隔进行补偿,如果我要更改选项卡或其他东西。请记住,第 3 步不起作用 但是 如果我在第 3 步之后更改选项卡然后 return 到我的原始页面(执行模糊)间隔将再次开始工作.
注意 除页面加载外,此处加载的所有内容都是通过 ajax 调用完成的。
我的代码:
正在初始化:
$.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function() {
$.automation.tanks.tableInit();
});
绑定函数:
bindIntervalEvent: function (target, url, callback) {
$(window)
.on("focus.mine",
function() {
$.automation.worker.setUpdateInterval(target, url, callback);
})
.on("blur",
function() {
$.automation.worker.stopUpdateInterval();
}).trigger("focus.mine");
}
区间函数:
setUpdateInterval: function (target, url, callback) {
if ($.automation.globals.globalInterval.value.length === 0) {
$.automation.globals.globalInterval.value.push(window.setInterval(
function () {
var options = {
loadTarget: target
}
$.automation.worker.getView(url,
function() {
if (callback)
callback();
},
options);
},
5000));
}
}
停止区间的函数:
stopUpdateInterval: function () {
if ($.automation.globals.globalInterval.value.length === 0)
return;
console.log("deleting");
for (var i = 0; i <= $.automation.globals.globalInterval.value.length; i++) {
window.clearInterval($.automation.globals.globalInterval.value[i])
$.automation.globals.globalInterval.value.splice(i, 1);
console.log($.automation.globals.globalInterval.value.length);
}
}
停止间隔时,我还删除了 window 绑定:
unBindIntervalEvent: function() {
$(window).off("focus.mine");
$(window).unbind("blur");
}
返回第 3 步:
我的 getviewfunction 回调中的成功方法与我一开始执行的相同 代码:
$(".updatelatest")
.on("click",
function () {
var _this = $(this);
var options = {
loadTarget:"#TanksContent"
}
$.automation.worker.getView("/Tank/GetTanks",
function (data) {
$(_this).switchClass("col-md-5", "col-md-1", 1000, function() {
$(_this).addClass("hidden");
$(".search").switchClass("col-md-5", "col-md-12", 1000, "easeInOutQuad");
})
$.automation.tanks.tableInit();
$.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function () {
$.automation.tanks.tableInit();
});
$(window).trigger("blur");
}, options);
});
但这不会开始间隔。它显然已初始化,因为它在执行 window.blur 时有效,例如当我更改选项卡时,但由于某种原因,它无法正常工作。
我尝试触发 windows 模糊事件但没有任何反应,我尝试触发我的自定义 window 事件 "focuse.mine" 但没有任何反应。
我在开发时没有注意到这一点,因为我打开了 firebug,每次我检查脚本或 css 或控制台时,都会执行模糊功能,所以我认为我的代码按预期工作但现在它已部署,我注意到了这一点。
我的脑袋莫名其妙地怦怦直跳,我想不出我哪里做错了。
嗯,这很有趣。我只是发现在调用 setUpdateInterval() 时;直接运行它给了我想要的结果。
我意识到我让它们像我那样分开的原因是因为模糊事件。 "Focus.mine" 被触发以在用户返回页面时再次开始间隔。