最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - disable page refresh on lose focus - Stack Overflow

matteradmin6PV0评论

I am wondering if there is a way to disable an automatic page refresh when a page loses focus. I have it setup to refresh when it gains focus again already using this:

window.onblur= function() {window.onfocus= function () {location.reload(true)}};

that I found from here. I originally had the page auto refresh by using:

<meta http-equiv="refresh" content="60"/>

Which reloads the page every 60 seconds regardless of state.

I want to make the page have the auto refresh only when in focus with the initial refresh ing when the page gains focus again. After the gain of focus it should refresh at the time interval until focus is lost.

Thanks

I am wondering if there is a way to disable an automatic page refresh when a page loses focus. I have it setup to refresh when it gains focus again already using this:

window.onblur= function() {window.onfocus= function () {location.reload(true)}};

that I found from here. I originally had the page auto refresh by using:

<meta http-equiv="refresh" content="60"/>

Which reloads the page every 60 seconds regardless of state.

I want to make the page have the auto refresh only when in focus with the initial refresh ing when the page gains focus again. After the gain of focus it should refresh at the time interval until focus is lost.

Thanks

Share Improve this question edited May 23, 2017 at 11:44 CommunityBot 11 silver badge asked Jun 15, 2013 at 21:36 thekgb09thekgb09 778 bronze badges 1
  • You could try to use Page Visibility API – w3/TR/page-visibility – gearsdigital Commented Jun 15, 2013 at 22:29
Add a ment  | 

2 Answers 2

Reset to default 5

You can't override this kind of refresh, you should probably use a JS timer to refresh, something like this (after removing the <meta http-equiv="refresh" content="60" /> tag):

var hasFocus;

window.onblur = function() {
    hasFocus = false;
}

window.onfocus = function(){
    hasFocus = true;
}

setInterval(reload, 60*1000);

function reload(){
    if(hasFocus){
        location.reload(true);
    }
}

I ended up modifying the code from Mostafa Torbjørn Berg to maintain the refresh on focus and have the page automatically refresh every 60 seconds while the page has focus.

var hasFocus= true;
window.onblur = function() {
    hasFocus = false;
}
window.onfocus = function(){
    location.reload(true);
}
setInterval(reload, 60*1000);
function reload(){
    if(hasFocus){
        location.reload(true);
    }
}
Post a comment

comment list (0)

  1. No comments so far