简体   繁体   中英

How can I reset my “Time spent on Page” Timer when my seconds go over 60 in Javascript

So I got an alertbox that shows your current time spent on the page.. the problem is that if it goes over 60 seconds it does not reset to 0 seconds. I dont know how to fix that and I didnt find any solution by searching... my current code:

var d = new Date();
var elapsed = new Date() - d;
var seconds = parseInt(elapsed / 1000);
var minutes = parseInt(seconds / 60);
alert("Time spent on page: " + minutes + " Minute/s " + seconds + " Scond/s");

Common logic problem. Just take the remainder.

var d = new Date();
var elapsed = new Date() - d;
var totalseconds = parseInt(elapsed  / 1000);  
var minutes = parseInt(totalSeconds / 60);
var seconds = totalSeconds % 60; //Take only remainder (modulus)

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