Browser Windows and tabs

Browser Windows and tabs

I have a timer function which ticks each second showing a counter. All pages on my app 2 for the example run this Js. 

The first problem is that I need that the timer runs only on the page I have the focus. I solve that with 

  $(window).blur(function () {

        TimerEndsFunction();

    });

    $(window).focus(function () {

        TimerStartsFunction;

    }); 

Now the problem is that I need that the timer steel runs in the last window I was using if i focus other application, the desktop or any other tap or window different to my app. 

Any suggestion?