1.2 KiB
		
	
	
	
	
	
	
	
			
		
		
	
	
			1.2 KiB
		
	
	
	
	
	
	
	
title
| title | 
|---|
| How to Create a Countdown Timer | 
How to Create a Countdown Timer
Creation
Start by building the countdownTimer function.
function startCountdown(seconds){
  var counter = seconds;
  var interval = setInterval(() => {
    console.log(counter);
    counter--;
    
    if(counter < 0 ){
      
      // The code here will run when
      // the timer has reached zero.
      
      clearInterval(interval);
      console.log('Ding!');
    };
  }, 1000);
};
Execution
Now to start the timer we provide startCountdown() with a number value as an argument which represents the seconds to countdown.
  startCountdown(10);
  // Console Output // 
  // 10
  // 9
  // 8
  // 7
  // 6
  // 5
  // 4
  // 3
  // 2
  // 1
  // 0 
  // Ding!
Live Example
More Resources
Methods used: