Search code examples
javascriptcountdown

JavaScript Countdown Timer not starting [Code Included]


I'm trying to make a countdown timer using JS, obviously.

I think my code is correct, but it does not work.

Objective of the code: The code suppose to show a timer decreasing until it show the content which is DONE in the alst line of the code.

Problem: I've tried making an HTML page on my local machine and tested it but it didn't work, also I've uploaded it on my website and it does not work too.

Code:

<body>

<div 
         id="JSPractice5" 
          style="border-style:dotted; 
          padding:10px; 
          font-size:24px; 
          width:200px; 
          text-align:center;">
Countdown Starting
</div>

<script type="text/javascript">
var containerID = "JSPractice5";
var number = 100;
var timerID = setInterval("CountdownTimer()",1000);
function CountdownTimer() {
if(number > 1) {
    number--;
    ReplaceContentInContainer(containerID,number); //Mark1
    }
else {
    clearInterval(timerID);
    ReplaceContentInContainer(containerID,'DONE!!');
    }
}
   </script>
</body>

If the solution of the problem is easy/stupid and you thought of down voting it, please don't do, because I'm very new to SOF and JS :)

Thanks in Advance guys.


Solution

  • You're missing

    function ReplaceContentInContainer(id, content)
    {
        document.getElementById(id).innerHTML = content;
    }