Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 413
Chathurika Avatar asked Apr 22 '26 15:04

Chathurika


1 Answers

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

like image 132
Sergej Avatar answered Apr 24 '26 06:04

Sergej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!