当焦点在其他窗口时停止运行Javascript Web应用程序的方法 [英] Way to stop the running of a Javascript web application when the focus is on other window

查看:49
本文介绍了当焦点在其他窗口时停止运行Javascript Web应用程序的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当重点放在其他窗口时,有没有办法停止运行Javascript Web应用程序?
例如,如果我在应用程序Web中执行了AJAX,那么在这种情况下非常有效地停止运行。

Are there any way to stop the running of a Javascript web application when the focus is on other window? For example, If I have AJAX executions in a application web, It'd very efficient stop the running in that situation.

推荐答案

页面可见性api的问题是,它不是跨浏览器兼容的。没有Safari,没有IE< 10。
https://developer.mozilla.org/en-US/docs / DOM / Using_the_Page_Visibility_API

Problem with the page visibility api is, that it's not cross browser compatible. No Safari, no IE<10. https://developer.mozilla.org/en-US/docs/DOM/Using_the_Page_Visibility_API

您可以尝试使用窗口焦点和模糊事件。 (jQuery示例)

You could try to use the windows focus and blur events. (jQuery Example)

(function() {
    var isFocused = true;
    $(window).focus(function() {
        isFocused = true;
    });

    $(window).blur(function() {
        isFocused = false;
    });

    function doSomeAjax() {
        if (isFocused) {
            doSomething();
        }
    }

});

它不像页面可见性api那样花哨,因为它只告诉你,标签是否集中或不是,但它可能足以实现,你想要做什么。

It's not that fancy as the page visibility api, because it only tells you, whether the tab is focused or not, but it might be enough to achieve, what you're trying to do.

这篇关于当焦点在其他窗口时停止运行Javascript Web应用程序的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆