您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JavaScript如何判断浏览器/标签页是否处于活动状态

JavaScript如何判断浏览器/标签页是否处于活动状态

您将使用窗口的focusblur事件:

var interval_id;
$(window).focus(function() {
    if (!interval_id)
        interval_id = setInterval(hard_work, 1000);
});

$(window).blur(function() {
    clearInterval(interval_id);
    interval_id = 0;
});

要回答“ Double Fire”的评论问题并保持在jQuery易用性之内:

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

    if (prevType != e.type) {   //  reduce double fire issues
        switch (e.type) {
            case "blur":
                // do work
                break;
            case "focus":
                // do work
                break;
        }
    }

    $(this).data("prevType", e.type);
})
javascript 2022/1/1 18:23:34 有454人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶