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

36 Views Asked by At

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");
1

There are 1 best solutions below

1
On BEST ANSWER

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)