0

In the following code, "Blast Off!" displays one second after the 1 displays. But, if I change the function call to setTimeout(finish(), 1000) "Blast Off!" displays immediately after 1. Why does including the () change the timing?

let countDown= time => {
    let timer = setInterval(function(){
        console.log(time);
        time--;
        if (time==0){
            clearInterval(timer);
            let finish = ()=> {
                console.log("Blast Off!");
            }
            setTimeout(finish, 1000);   
        }
    1000);
};
let startNum = Number(prompt("What number should I start counting down from? "));
countDown(startNum);

Trying to understand the difference between setTimeout(finish, 1000) and setTimeout(finish(), 1000)

danh
  • 62,181
  • 10
  • 95
  • 136
mjf1412
  • 1
  • 1

1 Answers1

0

When you use finish you are just telling the timeout that you want to run the finish function after 1000ms. On the other hand, when you use finish() you are calling the function, that is why it runs immediately

Boguz
  • 1,600
  • 1
  • 12
  • 25