Chathurika
Chathurika

Reputation: 419

Javascript Countdown timer function not working properly

I have developed a countdown timer function.

This function works fine. But the problem is it goes through the minus value too. So I want to stop the counting when its come to the 00:00:00. How can I do this.please help me?

Javascript

function initCountdown() {

 if( seconds == 0 && minutes == 0 && hours == 0 ){
      clearInterval( interval ); }
 if (seconds < 10) {
      outputElement.innerHTML = hours + ": " + minutes + ": " + "0" + seconds + " ";
 } else {
      outputElement.innerHTML = hours + ": " + minutes + ": " + seconds + " "; }} 
  function count(){
        time[2]--;
       if (time[2] == -1) {
          time[1]--;
          time[2] = 59
       }
    if (time[1] == -1) {
          time[0]--;
          time[1] = 59
         }
        print();
      }
   var outputElement = document.getElementById('demo');
   var time = document.getElementById("picker-dates1").value;
   time = time.split(':'); }

HTML

 <input type = "text" id = "picker-dates1"/>
 <P id="demo"></p>

Upvotes: 3

Views: 130

Answers (1)

Sergej
Sergej

Reputation: 2186

Replace print(); with if (time[0] >= 0) { print(); }

Upvotes: 1

Related Questions