jQuery - Countdown with Play/Pause
Try to make it pure JavaScript for IE9+
by katalin_2003
HTML
<a href="#" id="play">Play</a>
<a href="#" id="pause">Pause</a>
<h1 id="output">Seconds: 10</h1>
JavaScript
// with JavaScript
var play = document.getElementById('play'),
pause = document.getElementById('pause'),
output = document.getElementById('output'),
isPaused = false,
time = 10;
var t = window.setInterval(function () {
if (!isPaused) {
time--;
output.innerHTML = "Seconds: " + time;
if (time === 0) {
isPaused = true;
console.log('stop time');
}
}
}, 1000);
pause.onclick = function (e) {
e.preventDefault();
isPaused = true;
console.log('pause');
};
play.onclick = function (e) {
e.preventDefault();
if (time !== 0) {
isPaused = false;
console.log('play');
} else {
console.log('can\'t go back in time :(');
}
};