[英]How to start javascript setinterval once cleared?
I have a setinterval that runes every 5 seconds. 我有一个setinterval每5秒运行一次。 this works fine on page load.
这在页面加载时效果很好。
I have the following scenarios: 我有以下几种情况:
NOTE all content loading here exept page load is done with ajax calls. 注意,这里所有的内容加载(例如页面加载)都是通过ajax调用完成的。
My code: 我的代码:
initializing : 初始化 :
$.automation.worker.bindIntervalEvent("#TanksContent", "/Tank/GetTanks", function() {
$.automation.tanks.tableInit();
});
binding function: 绑定功能:
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");
}
interval function: 间隔函数:
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));
}
}
the function that stops the interval: 停止时间间隔的功能:
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);
}
}
when stopping the interval i also remove the window bindings: 当停止间隔时,我也删除了窗口绑定:
unBindIntervalEvent: function() {
$(window).off("focus.mine");
$(window).unbind("blur");
}
Back to step 3: 返回步骤3:
My sucess method in the callback to my getviewfunction is identical to what i execute in the beginning code: 我的getview函数回调中的成功方法与我在开始代码中执行的方法相同:
$(".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);
});
but this does not start the interval. 但这不会开始间隔。 it is clearly initialized since it works when window.blur is executed for example when I change tab but for some reason this is not working beyond that.
它显然已初始化,因为它在执行window.blur时(例如,当我更改tab时)有效,但由于某种原因,它无法正常工作。
i tried triggering the windows blur event and nothing happened, i tried triggering my custom window event "focuse.mine" but nothing happens. 我试图触发Windows模糊事件,但没有任何反应,我试图触发自定义窗口事件“ focuse.mine”,但没有任何反应。
I did not notice this while developing since I had firebug open and every time i checked scripts or css or the console the blur function was executed so I assumed that my code worked as intended but now that it is deployed I notice this. 我在开发过程中没有注意到这一点,因为我已经打开了萤火虫,并且每次我检查脚本,css或控制台时,都会执行模糊功能,因此我以为我的代码可以按预期工作,但是现在部署它时,我注意到了这一点。
My head is pounding beyond reason and I can't for figure out where I have gone wrong. 我的头跳动得超出理智,我无法弄清楚哪里出了问题。
Well this was a fun one. 好吧,这很有趣。 I simply found that when calling the setUpdateInterval();
我只是发现在调用setUpdateInterval()时发现了这一点。 function directly it gave me the desiered result.
直接功能它给了我想要的结果。
I realized that the reason I had them split like I did was becaouse of the blur event. 我意识到,像我一样将它们拆分的原因在于模糊事件。 "Focus.mine" is triggered to start the inteval again ocne a user comes back to the page.
当用户返回页面时,将触发“ Focus.mine”以再次启动间隔。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.