0

Till date, we have requirement like whenever user is active on a page, we need to do auto refresh of this page. Whenever user minimized/active on another window, we need to stop auto refresh.

User is come up with new requirement. We need to auto-refresh page whenever it is visible to user. It's like i have set 2 windows horizontally/vertically so that i can work on one window and can review another window. How can I achieve this functionality?

Thanks in advance.

alok_dida
  • 1,723
  • 2
  • 17
  • 36
  • @ZathrusWriter See his questions, there is no answer to be accepted. – Ram Sep 17 '12 at 10:19
  • @undefined you can always close, self-answer or delete a question without an answer... you can even start a bounty if there aren't enough answers or attention – Zathrus Writer Sep 17 '12 at 10:39
  • @ZathrusWriter Yes, sorry but your comment is not constructive. – Ram Sep 17 '12 at 10:41
  • @ZathrusWriter . I am also agree with undefined. If i am not able to find solution than how can i "Self-answer" it. I am expecting more answers for this question so that how can i "delete" this question. This issue still open so that how can I "close" this question. Please give me answers. – alok_dida Sep 17 '12 at 10:51
  • @alok_dida you can always start a bounty for it - that always worked in 100% of my questions... your questions are 0.5 to 1 year old, they are unlikely to be answered if you don't feature them yourself (via a bounty) – Zathrus Writer Sep 17 '12 at 11:00

2 Answers2

0

Take a look at this answer: JavaScript / jQuery: Test if window has focus for a method to determine if window has focus.

In the setInterval part of the above example you can add code to refresh the page.

You could add a call to

location.reload();

for refreshing the page

Community
  • 1
  • 1
mortb
  • 9,361
  • 3
  • 26
  • 44
  • Currently I have implemented in this way only. But now user don't have focus on the page but still it's visible to user. scenario: I have opened 2 pages. Page 1 and Page 2. I want to autorefresh Page1 on regular interval. I have set both pages vertically/horizontally, I am actively working on Page 2 and reviewing the Page 1. So focus is on Page 2 only not on Page 1 but still i want to refresh Page 1 as it is visible to user. – alok_dida Sep 17 '12 at 10:20
  • Oh, sorry for not reading your question properly! Steven Lizarazo answers seems to come from this post: http://stackoverflow.com/questions/1060008/is-there-a-way-to-detect-if-a-browser-window-is-not-currently-active have a look at it to see if it can help you – mortb Sep 17 '12 at 11:17
0

Try some like this to detect visibility:

var state='';



    (function() {
        var hidden, change, vis = {
                hidden: "visibilitychange",
                mozHidden: "mozvisibilitychange",
                webkitHidden: "webkitvisibilitychange",
                msHidden: "msvisibilitychange",
                oHidden: "ovisibilitychange" /* not currently supported */
            };             
        for (hidden in vis) {
            if (vis.hasOwnProperty(hidden) && hidden in document) {
                change = vis[hidden];
                break;
            }
        }
        if (change)//this is for detecting without  focus, so it works for your second requirement
            document.addEventListener(change, onchange);
        else if (/*@cc_on!@*/false) // IE 9 and lower
            document.onfocusin = document.onfocusout = onchange
        else
            window.onfocus = window.onblur = onchange;

        function onchange (evt) {
            var body = document.body;
            evt = evt || window.event;

            if (evt.type == "focus" || evt.type == "focusin")
           {   
        body.className = "visible";
           state='visible';
            }         else if (evt.type == "blur" || evt.type == "focusout")
            {  body.className = "hidden";     state='';}
                else    {    
                body.className = this[hidden] ? "hidden" : "visible";}
        }

    })();


function timeout_trigger() {
if(state=='visible')
{
//    window.open('PAGE','_SELF'); OR some ajax  
}
}


    setTimeout('timeout_trigger()', 30000);

onfocusin and onfocusout are required for IE 9 and lower, while all others make use of onfocus and onblur.

Steven Lizarazo
  • 5,320
  • 2
  • 28
  • 25