info343/minilabs/6/solution/minilab6-ex4.js

// minilab6-ex4.js
// Sample JavaScript solution for Mini-Lab 6: Timers
// (Partial solution through Exercise 4)
// INFO 343, Autumn 2012
// Morgan Doocy

/*[<ins>]*/var timer = null;/*[</ins>]*/

// Attach click handler to #begin button when the document is ready.
$(document).ready(function() {
   $('#begin').click(startTimer);
});

// Begin the countdown timer.
function startTimer() {
   /*[<ins>]*/timer =/*[</ins>]*/ setInterval(countDownByOne, 1000);
}

// Decrement the number in #seconds by one. /*[<ins>]*/If the number is zero, stop the
// countdown./*[</ins>]*/
function countDownByOne() {
   var seconds = $('#seconds').val();
/*[<ins>]*/   if (seconds > 0) {/*[</ins>]*/
      $('#seconds').val(seconds - 1);
/*[<ins>]*/   } else {
      clearInterval(timer);
      timer = null;
   }/*[</ins>]*/
}