Having some difficulty with resetting a counter in Javascript that I found on Stack Overflow. Here is the code for the counter:
var pageVisited = new Date(); // consider storing this data in a database, session, or at least a cookie
setInterval(function() {
var timeOnSite = new Date() - pageVisited;
var secondsTotal = timeOnSite / 1000;
var seconds = Math.floor(secondsTotal);
document.getElementById('time').innerHTML = seconds;
}, 1000);
I have tried the following based on suggestions from other posts:
function stopTime(){
clearInterval(timeOnSite);
}
Unfortunately, this method restarts the counter but causes two counters to run simultaneously.
My goal is simply to reset the counter back to 0 and start again when the user clicks 'Update' (data update functionality is already working).
Any help or recommended resources would be greatly appreciated.
Thank you