[Solved] Simple Explanation To Make A Countdown using Recursion!(Javascript) [closed]


A count down method by recursion. The biggest problem is slowing the countdown so you can actually see the numbers change. For that we can use setInterval.

so the code below is pretty simple. First define the end case, in this case when n=0. Update the innerHTML and quit (don’t return anything). ELSE return the recursive call to countdown(n-1). since we do that inside of setInterval we need to make sure we clear setinterval

var count = document.getElementById('count');
var interval;
function countDown(n){

clearInterval(interval);

if(n==0){
  
   count.innerHTML = n
 }else{    
    count.innerHTML=n;    
    interval=setInterval(function(){    
    return countDown(n-1);
    },500);
}}

countDown(10);
<div id='count'></div>

1

solved Simple Explanation To Make A Countdown using Recursion!(Javascript) [closed]