javascript - How to reset the value of one variable from one or more tab? -


i have 2 tab. 1 home tab , 1 home-1 tab. here tracking time count login logout time. have following code in 1 java script file. work 2 instance.

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

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

any key/mouse event reset 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 = 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 include above lines in java script file. trigger mouse/key event on home tab reset counter value in home-1 tab.

using 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 write or wrong. home tab reset counter value home-1 , other tabs. other tabs home-1 tab not reset home tab , other tabs. reason? , how reset other tab counter value working tab?

well can write functionality in such way that, store value in html5 localstorage when something.

if (typeof(storage) !== "undefined") {     localstorage.setitem("reset", "true");  } 

and if not doing set false.

localstorage.setitem("reset", "false"); 

you can check these values :

localstorage.getitem("reset"); 

so when have different tabs of same domain, can check out , see if did in tab.

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

hope helps.


Comments