I'm creating online exam kind of application and in my app
when user clicks on start button we are going to display timer for 3:00 minutes
but when I refresh the page the timout function is resetting, how to stop this
how to do this , this is a must have feature in my app.
If you need to achieve this in a foolproof way that the user can't tamper with, you'll have to set the timer serverside.
If it's not that important, then you can store the start-time in localStorage and, on page load, use that value to resume the timer from where it began.
var oldStartTime = localStorage.getItem('startTime'); var startTime = oldStartTime ? new Date(oldStartTime) : new Date(); localStorage.setItem('startTime', startTime); // you can now use startTime to determine the remaining duration for setTimeout: var elapsed = new Date() - startTime; var duration = 180000 - elapsed; setTimeout(myFunction, duration);