0

我有一个应用于 setInterval 函数的函数。当我最小化或更改焦点窗口,然后返回显示我的网站的浏览器时,浏览器会以非常快的方式播放自从我将焦点更改到另一个窗口后发生的所有事情。

当窗口中的焦点窗口改变时,有没有办法保持动画,setintervals?

谢谢。

4

3 回答 3

0

我找到了这篇文章: JavaScript / jQuery: Test if window has focus

对我来说,它适用于谷歌浏览器,但可能是它在某些浏览器中不起作用。

这是一个要测试的小提琴:

http://jsfiddle.net/ScKbk/

他的回答:

var window_focus;

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

.blur(function() {
    window_focus = false;
});

$(document).one('click',function() {
   setInterval(function() { $('body').append('has focus? ' + window_focus + '<br>'); }, 1000);
});​
于 2013-10-03T08:52:16.327 回答
0

尝试这个。

    var handeler;
    function ShowAnimation()
{
    //SetInterval code
    handeler = SetInterval(myfunction, 1000);
}



//clear the handler when not in use.
function Clearhandler()
{
 ClearTimeout(handeler);
}



 //call the above method on the onblur event of window.
  $(window).focus(ShowAnimation(),Clearhandler());
于 2013-10-03T08:53:25.487 回答
0

很像 Getu.ch 的答案,除了这只会在窗口有焦点时执行你的“工作”代码(每 3 秒运行一次)。未在所有浏览器中测试,但这里有一个链接显示 window.focus / window.blur 的浏览器兼容性

 (function($) {

     var windowHasFocus = false;

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

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

     setInterval(function() { 
         if(windowHasFocus) {
            //Do your work
         }
     }, 3000);

});
于 2013-10-03T09:08:26.390 回答