valentincognito
1/17/2017 - 7:48 AM

Simple Countdown Timer

Simple Countdown Timer

function startTimer(duration, display) {
    var timer = duration, minutes, seconds;
    var interval_handle = setInterval(function () {
      minutes = parseInt(timer / 60, 10);
      seconds = parseInt(timer % 60, 10);

      minutes = minutes < 10 ? "0" + minutes : minutes;
      seconds = seconds < 10 ? "0" + seconds : seconds;

      display.text(minutes + ":" + seconds);

      if (--timer < 0) {
        clearInterval(interval_handle);
      }
    }, 1000);
  }

  $(document).ready(function(){

    $(function ($) {
      var fiveMinutes = 60 * 5,
        display = $('.timer');
      startTimer(fiveMinutes, display);
    });

  });/* document ready end */