You can use localStorage or Cookie. Cookie is best used with Server-Side communication, where you can monitor user activity if they try to cheat on countdown. LocalStorage is on the Client-Side, so the client can manipulate at their will. However, both ways are possible, please have a look at below sample that is quiet messy, sorry. Hope this helps you. By the way, cookies won't work on localhost, your page should run on node.js or on similar environment.
HTML:
<div id="time"></div>
JS
var _time = 0;
var _cookie_val = {hours:"", minutes:"", seconds:""};
//change seconds to HH:MM:SS
//http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-string-with-format-hhmmss
String.prototype.toHHMMSS = function () {
var sec_num = parseInt(this, 10);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
var seconds = sec_num - (hours * 3600) - (minutes * 60);
if (hours < 10) {hours = "0"+hours;}
if (minutes < 10) {minutes = "0"+minutes;}
if (seconds < 10) {seconds = "0"+seconds;}
return {hours:hours, minutes:minutes, seconds:seconds};
}
//get Cookie
let _getCookie = function(args) {
var _cookie_Val = "; " + document.cookie;
var _splits = _cookie_Val.split("; " + args + "=");
if (_splits.length==2) return _splits.pop().split(";").shift();
}
//set cookie
let _cookie = function (args) {
document.cookie = "_hours=" + args.args_toHHMMSS.hours;
document.cookie = "_minutes=" + args.args_toHHMMSS.minutes;
document.cookie = "_seconds=" + args.args_toHHMMSS.seconds;
document.cookie = "args=" + args.args;
};
var _timer = function (args) {
//set _time to args if _getCookie('args') is undefined or empty
var _time = _getCookie("args") || args;
//convert val to string and set cookie
_cookie({args_toHHMMSS: _time.toString().toHHMMSS(), args: _time});
//get cookie _getCookie(_hours || _minutes || _seconds || args)
// console.log(_getCookie("args").toString().toHHMMSS());
var update_print = _getCookie("_hours") + ":" +
_getCookie("_minutes") + ":" +
_getCookie("_seconds");
document.querySelector('#time').textContent = update_print;
//start interval
let _interval = setInterval( function(){
//convert val to string and set cookie
_cookie({args_toHHMMSS: _time.toString().toHHMMSS(), args: _time});
//get cookie _getCookie(_hours || _minutes || _seconds || args)
var update_print = _getCookie("_hours") + ":" +
_getCookie("_minutes") + ":" +
_getCookie("_seconds");
document.querySelector('#time').textContent = update_print;
//stop interval when time reaches 0
if(_time <= 0){
//finite loop
clearInterval(_interval);
//infinite loop
//_time = args;
}else{ _time--; }
},1000);
}
//onload this function will be executed
window.onload = () => {
var seconds = 5; //enter the desired minute. e.g 300 for 5 min
_timer(seconds); //call _timer with argument seconds
};