0

I have two tab. One is home tab and another one is home-1 tab. Here i tracking the time count for login to logout time. I have following code in one java script file. this work as two instance.

home tab -> www.example.com/home //(my java script file contain this directory) first instance.

home-1 tab -> www.example.com/home-1 //(required script file include from home tab into this tab) second instance.

Any key/mouse event will reset the counter.

var sestmout=0;
var idletm=60*1;
var idletmcntr=0;
var idletimeout=null;
var lastUpdate1 = new Object();
var lastUpdate = 0;
var curdttm = new Date();

$(document).ready(function(){
    initidletm();
});

lastUpdate1.cntr = idletmcntr;  
lastUpdate1.ctme = curdttm.getMilliseconds();
lastUpdate1.time = curdttm.getTime();

function reset_cnt(){
if(idletmcntr != 0){
        curdttm = new Date();
        lastUpdate1.cntr = idletmcntr;  
        lastUpdate1.ctme = curdttm.getMilliseconds();
        lastUpdate1.time = curdttm.getTime();
        var a = moment();
        lastUpdate1.utme = a.valueOf();
    }
    idletmcntr=0;
}

document.onclick=function(){
    reset_cnt();

};
document.onmousemove=function(){
    reset_cnt();
};
document.onkeypress=function() {
    reset_cnt();
};

In each tab I include above lines in java script file. Now I trigger the mouse/key event on home tab that reset counter value in home-1 tab.

Using this code.

function initidletm(){
if(window.name == "home-1"){
    window.opener.document.onkeypress = document.onkeypress;
    window.opener.document.onclick = document.onclick;
    window.opener.document.onmousemove = document.onmousemove;

}else if(window.name == "home-2"){
    window.opener.document.onkeypress = document.onkeypress;
    window.opener.document.onclick = document.onclick;
    window.opener.document.onmousemove = document.onmousemove;
}
else{           // home tab
    document.onkeypress=function() {
        reset_cnt();
    };
    document.onclick=function() {
        reset_cnt();
    };
    document.onmousemove=function() {
        reset_cnt();
    };

} 
}
clearTimeout(idletimeout);
idletimeout=setTimeout(trackidletm,1000);

function trackidletm() {
    initidletm();
}

This approach is write or wrong. Only home tab reset the counter value to the home-1 and other tabs. The other tabs like home-1 tab does not reset the home tab and other tabs. What reason? And How to reset the other tab counter value from working tab?

Jeyaprakash
  • 151
  • 3
  • 11
  • Do you know about `localStorage`?. You can write your status in the local storage and check it out in another tab by reading it. – dvenkatsagar Jan 29 '16 at 07:10
  • why do not use cookies.Store the tracking time count in the cookie and reset when ever you want to do from the tabs.[Link][http://stackoverflow.com/questions/1599287/create-read-and-erase-cookies-with-jquery] – shu Jan 29 '16 at 07:32

1 Answers1

1

Well you can write a functionality in such a way that, it would store a value in the HTML5 localStorage when you do something.

if (typeof(Storage) !== "undefined") {
    localStorage.setItem("reset", "true"); 
}

And if you not doing anything it will set it to false.

localStorage.setItem("reset", "false");

You can check these values like this :

localStorage.getItem("reset");

So when you have different tabs of the same domain, you can check out and see if you did anything in another tab.

Ref : http://www.w3schools.com/html/html5_webstorage.asp

Hope it helps.

dvenkatsagar
  • 936
  • 7
  • 22