简体   繁体   English

清除后如何启动javascript setinterval?

[英]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: 我有以下几种情况:

  1. Load page with interval ( WORKS ) 间隔加载页面( WORKS
  2. press button and load new content and stopp interval( WORKS ) 按下按钮并加载新的内容和STOPP间隔( 工程
  3. Once the new content is no longer desiered, dissmiss it, return to first content and start interval again( DOES NOT WORK ) 一旦不再需要新内容,请将其解散,返回到第一个内容,然后重新开始间隔( 不起作用
  4. I have saftys suchs as events for window.blur that also stops the interval so that the browser does not commponsate for all the missing intervals if i would change tabs or something. 我有诸如window.blur之类的事件之类的安全性,它也可以停止间隔,因此如果我要更改标签或其他内容,浏览器将不会对所有缺少的间隔进行提示。 Keep in mind that step 3 did not work BUT if i would after step 3 change a tab and then return to my original page(execute blur) the interval would start working again. 我是否会步三改一选项卡,然后回到我原来的页面后(执行模糊)的时间间隔会重新开始工作请记住,第3步没有工作,

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM