Skip to content

Instantly share code, notes, and snippets.

@mertkahyaoglu
Created November 15, 2017 06:46
Show Gist options
  • Save mertkahyaoglu/e8733dfd76908c494b7b19cc5f7ee97d to your computer and use it in GitHub Desktop.
Save mertkahyaoglu/e8733dfd76908c494b7b19cc5f7ee97d to your computer and use it in GitHub Desktop.
<span class="timeout">2:00</span>
<button id="startTimer">Start</button>
<script>
var interval;
function countdown() {
clearInterval(interval);
interval = setInterval( function() {
var el = $('.timeout')
var timer = el.html();
timer = timer.split(':');
var minutes = timer[0];
var seconds = timer[1];
seconds -= 1;
if (minutes < 0) return;
else if (seconds < 0 && minutes != 0) {
minutes -= 1;
seconds = 59;
}
else if (seconds < 10 && length.seconds != 2) seconds = '0' + seconds;
el.html(minutes + ':' + seconds);
if (minutes == 0 && seconds == 0) {
clearInterval(interval);
// do other stuff when timeout
}
}, 1000);
}
$('#startTimer').click(function () {
$('.timeout').text("2:00");
countdown();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment