最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why is $(document).blur() and $(document).focus() not working with Safari or Chrome? - Stack Overflow

programmeradmin1浏览0评论

I am making a counter which counts down when document is on focus. It stops counting down when it's on blur.

It is working in FF, but with Safari and Chrome, the counter doesn't work at all.

Is there a patibility problem with Safari/Chrome?

All I'm using is $(document).blur() and $(document).focus(), and there are both within a $(document).ready() block.

var tm;
$(document).ready(function(){   

        var seconds = 50;
        $('#timer').html(seconds);
        countdown();

    $(window).focus(function(){
         function countdown(){ 
         if (seconds > 0) {
            seconds--; 
            $('#timer').text(seconds);
            tm = setTimeout(countdown,1000);
            }
        if (seconds<=0){ 
            $('#timer').text('Go');
            }   
        }); 



    $(window).blur(function(){
        clearTimeout(tm);
        seconds++;
        $('#timer').text(seconds);

    });
});

I am making a counter which counts down when document is on focus. It stops counting down when it's on blur.

It is working in FF, but with Safari and Chrome, the counter doesn't work at all.

Is there a patibility problem with Safari/Chrome?

All I'm using is $(document).blur() and $(document).focus(), and there are both within a $(document).ready() block.

var tm;
$(document).ready(function(){   

        var seconds = 50;
        $('#timer').html(seconds);
        countdown();

    $(window).focus(function(){
         function countdown(){ 
         if (seconds > 0) {
            seconds--; 
            $('#timer').text(seconds);
            tm = setTimeout(countdown,1000);
            }
        if (seconds<=0){ 
            $('#timer').text('Go');
            }   
        }); 



    $(window).blur(function(){
        clearTimeout(tm);
        seconds++;
        $('#timer').text(seconds);

    });
});
Share Improve this question edited Jan 28, 2018 at 12:02 halfer 20.4k19 gold badges108 silver badges201 bronze badges asked Apr 25, 2012 at 23:17 alexx0186alexx0186 1,5675 gold badges20 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 13

I've always used $(window).focus() and $(window).blur(). Try these instead.

Also, note that in FF and IE the "focus" event fires on ~document load, while in Chrome and Safari it only fires if the window had lost focus before and now it has regained it.

UPD: Now as you pasted your code, I reworked it to (hopefully) fit your purpose:

var tm;
var seconds = 50;
var inFocus = true;

function countdown() {
    if (seconds > 0) {
        seconds--;
    }

    if (seconds <= 0) {
        $('#timer').text('Go');
    }
    else {
        $('#timer').text(seconds);
        tm = setTimeout(countdown, 1000);
    }
}

$(function() {
    $('#timer').html(seconds);
    countdown();

    $(window).focus(function() {
         if(!inFocus) {
             countdown();
         }
    });

    $(window).blur(function() {
        inFocus = false;
        clearTimeout(tm);
        seconds++;
        $('#timer').text(seconds);
    });
});
发布评论

评论列表(0)

  1. 暂无评论