Explain setTimeout function and callback function Dice Game - Step 62

I understand that once the game ends, after 500ms, I would be displaying the alert message. But can someone explain to me why do I need to include an arrow function?
Why cant it be written like this?

 setTimeout({`Game Over! Your total score is ${totalScore}`); 500);

setTimeout gets a function as first argument, something that can be executed at the right time, if it’s not a function you get an error

1 Like

As said it takes a function, more specifically a callback function (it can also take a code string).

All setTimeout does is invoke the callback. The callback can have whatever code you need to run with a timeout. In this case, the setTimeout callback will be used to call two other functions, alert and resetGame.

Also, passing data directly to setTimeout wouldn’t make sense, I mean what would it do with it? It is just a function used to delay some execution.


It is a standard API design because it is versatile. A function can do anything, contain a lot of code and data, and interact with the outside. If all the API accepted was data it would be a lot more constrained in how and what it was used for.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.