I'am creating a countdown timer.So this is the my code so far.
function countdownTimeStart() {
var el = document.getElementById('demo');
var cancel = document.getElementById('cancel');
/* Start count the time in timer panel */
var time = document.getElementById("picker-dates").value;
time = time.split(':');
var x = setInterval(function () {
// set hours, minutes and seconds, decrease seconds
var hours = time[0];
var minutes = time[1];
var seconds = time[2]--;
console.log(time);
// create a function to handle the cancel
function cancelCountdown(){
el.innerHTML = "00:00:00";
clearInterval(x);
}
// attach listener to cancel if cancel button is clicked
cancel.addEventListener( 'click', cancelCountdown);
// if seconds are negative, set them to 59 and reduce minutes
if (time[2] == -1) {
time[1]--;
time[2] = 59
}
// if minutes are negative, set them to 59 and reduce hours
if (time[1] == -1) {
time[0]--;
time[1] = 59
}
// Output the result in an element with id="demo"
if( seconds == 0 && minutes == 0 && hours == 0 ){
clearInterval(x);
el.innerHTML = "00:00:00";
} else if (seconds < 10) {
el.innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
} else {
el.innerHTML = hours + ": " + minutes + ": " + seconds + " ";
}
}, 1000);}
So I want to create a pause button for this. I refered similar questions such as Javascript - Pausing setInterval().
It seems that it is easy to create pause option in jquery. But I haven't idea how can I apply this to my script. Can someone help me.