Writing a simple seconds countdown timer with vanilla JavaScript

It’s the final countdown:

10

For this script, all you need to do is surround the number you want to start counting down with:

<div id="countdown">10</div>

Then use the following JavaScript:

<script type="text/javascript">
  var seconds;
  var temp;

  function countdown() {
    seconds = document.getElementById('countdown').innerHTML;
    seconds = parseInt(seconds, 10);

    if (seconds == 1) {
      temp = document.getElementById('countdown');
      temp.innerHTML = "all done, bye bye";
      return;
    }

    seconds--;
    temp = document.getElementById('countdown');
    temp.innerHTML = seconds;
    timeoutMyOswego = setTimeout(countdown, 1000);
  } 

  countdown();
</script>

7 thoughts on “Writing a simple seconds countdown timer with vanilla JavaScript”

What do you think?