简体   繁体   English

jQuery-如果浏览器标签集中,则执行AJAX-不起作用

[英]jQuery - If browser tab is focused, then do AJAX - Not working

I have a code that determines if current browser window or tab is active. 我有一个确定当前浏览器窗口或选项卡是否处于活动状态的代码。 If it's active, the title of the tab says "active" and if not it says "blurred" 如果处于活动状态,则选项卡的标题为“活动”,否则为“模糊”

It is working fine. 一切正常。 Here's the code: 这是代码:

 $(window).on("blur focus", function (e) {
            var prevType = $(this).data("prevType");

            if (prevType != e.type) {   //  reduce double fire issues

                if (e.type == "blur") {
                    document.title = 'blurred';
                } else if (e.type = "focus") {
                  document.title = 'focus';
                }
            }

            $(this).data("prevType", e.type);
        })

The code above is working fine. 上面的代码工作正常。

Now if I add AJAX to it, it doesn't work. 现在,如果我向其中添加AJAX,它将无法正常工作。

 $(window).on("blur focus", function (e) {
            var prevType = $(this).data("prevType");

            if (prevType != e.type) {   //  reduce double fire issues

                if (e.type == "blur") {
                    document.title = 'blurred';
                } else if (e.type = "focus") {

                    var interval = function () {
                        $.ajax({
                            url: "<?php echo base_url('home/get') ?>",
                            cache: false,
                            success: function (html) {
                                $("#text").val(html);
                                document.title ='focus';
                            },
                        });
                    };


                    setInterval(interval, <?php echo $int ?>);
                }
            }

            $(this).data("prevType", e.type);
        })

It says focused if it's in focus. 如果聚焦,则说聚焦。 If I go out of focus, it says "blurred" for less than a second, then says focus again. 如果我没有聚焦,它会说“模糊”不到一秒钟,然后再次聚焦。 I don't know why. 我不知道为什么 I want it to say blurred if it's not in focus. 我想说的不是很模糊,那就是模糊的。 Adding the AJAX code doesn't make it work. 添加AJAX代码无法使其正常工作。

Please help. 请帮忙。 Thanks. 谢谢。

You need to use clearTimeout() in your blur event. 您需要在blur事件中使用clearTimeout() My code continuously polls my server for data, but when I go out of the page, it stops polling. 我的代码不断轮询服务器中的数据,但是当我离开页面时,它将停止轮询。 Please look at the implementation below. 请查看下面的实现。 I have done the similar one in my application here : 我在这里的应用程序中做了类似的操作:

$(window).blur(function() {
    clearTimeout(getJsonTmr);
    clearTimeout(updatePreviewTmr);
}).focus(StartTimers);

function StartTimers () {
    // Every half a second, 
    getJsonTmr = setInterval(function () {
        $.get("/doodles/update?getJson&DoodleID=" + DoodleOptions.DoodleID, function (data) {
            data = JSON.parse(data);
            if (!DoodleOptions.isActive)
                clearDoodleCanvas();
            $.each(data, function (index) {
                drawFromStream(data[index]);
            });
        });
    }, 500);

    updatePreviewTmr = setInterval(function () {
        $.post("/doodles/update?updatePreview", {
            "DoodleID": DoodleOptions.DoodleID,
            "DoodlePreview": canvas.toDataURL()
        });
    }, 5000);
}
StartTimers();

You can use the above code as reference and change yours. 您可以使用上面的代码作为参考并进行更改。

A simple reference implementation for you... 一个简单的参考实现为您...

function timers() {
  tmrAjax = setInterval(function () {
    $.get(/* ... */);
  }, 1000);
}

timers();

$(window).blur(function () {
  clearInterval(tmrAjax);
}).focus(timers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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