Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,890 questions

51,819 answers

573 users

How to create countdown timer in JavaScript

1 Answer

0 votes
const countDownTime = new Date().getTime() + 24 * 60 * 6;

let val = setInterval(function() {
      const now = new Date().getTime();
      const timeLeft = countDownTime - now;

      const days = Math.floor(timeLeft / (1000 * 60 * 60 *24));
      const hours = Math.floor( (timeLeft / (1000 * 60 * 60)) % 24);
      const minutes = Math.floor((timeLeft / 1000 / 60) % 60);
      const seconds = Math.floor((timeLeft / 1000) % 60);
			
      console.log(days + "d " + hours + "h " + minutes + "m " + seconds + "s");
      
      if (timeLeft < 0) {
          clearInterval(val);
      }
    	
   }, 1000);
   
   
   
   
   
/*
run:
   
"0d 0h 0m 7s"
"0d 0h 0m 6s"
"0d 0h 0m 5s"
"0d 0h 0m 4s"
"0d 0h 0m 3s"
"0d 0h 0m 2s"
"0d 0h 0m 1s"
"0d 0h 0m 0s"
"-1d -1h -1m -1s"
   
*/

 



answered Jan 24, 2022 by avibootz

Related questions

1 answer 81 views
1 answer 75 views
1 answer 86 views
1 answer 76 views
1 answer 78 views
1 answer 88 views
...