简体   繁体   中英

jquery timeout function is not being called again after one call to it

Please Help me. I have to find the total time user has done activity on page. I have made the jquery code, this code finds if user sitting idle for 10 seconds, code increase its idle time. But problem with this code is, when user is on that page sitting idle (say 30 seconds) continuously, Code has to add 10 sec + 10 sec +10 sec total 30 sec in idle time. It adds only 10 seconds in idle time, skipping rest of the 20 sec as example i mentioned above. Altough it works fine when user done some activity like mousemove etc on page after timeout, it adds again 10 seconds to idletime, but again this time 10 seconds only, not full time span.

Issue could be with timeout. timeout function is not being called again after one call to it. successive timeout calls are not being made i think. i am not sure.

here is the code

 var timeoutID; var start, end, pagefocustime = 0; var opentime, midtime = 0; var exacttime = 0; var inactivetime = 10000, idletime = 0; var window_focus = true; $(document).ready(function() { start = performance.now(); //Calculating Startup Time midtime = start; $(window).on('blur', function() { window_focus = false; end = performance.now(); pagefocustime += end - midtime; exacttime += end - midtime; }) $(window).on('focus', function() { window_focus = true; midtime = performance.now(); }) if(window_focus){ function setup() { this.addEventListener("mousemove", resetTimer, false); this.addEventListener("mousedown", resetTimer, false); this.addEventListener("keypress", resetTimer, false); this.addEventListener("DOMMouseScroll", resetTimer, false); this.addEventListener("mousewheel", resetTimer, false); this.addEventListener("touchmove", resetTimer, false); this.addEventListener("MSPointerMove", resetTimer, false); startTimer(); } setup(); function startTimer() { // wait 10 seconds before calling goInactive timeoutID = window.setTimeout(goInactive, 10000); } function resetTimer(e) { window.clearTimeout(timeoutID); goActive(); } function goInactive() { idletime = idletime + inactivetime; console.log("I am called"); } function goActive() { startTimer(); } } $(window).on('beforeunload',function(){ end = performance.now(); pagefocustime += end - midtime; console.log("Total page focus Time : " + pagefocustime); opentime = end - start; console.log("Total Page Open time : " + opentime); exacttime = pagefocustime - idletime; console.log("exact time : " + exacttime); }); }); 

Please help. Many thanks in advance

You could call 'startTimer' inside of your 'goInactive' method to start the check again.

While you're in that code you could put all of those event names into an array and then use the map function to attach the listener.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM