JavaScript setTimeout - Interview Question...
By default, all JavaScript code runs synchronously. This means that the statements are evaluated from top to bottom, one after another.
The strings will be printed to the console in the same order as they appear.
Starting the timer with setTimeout
To delay the execution of some function in JavaScript, you can setTimeout
. In the base case it accepts two parameters:
callback
- the function that should be calleddelay
- in milliseconds
setTimeout
sets the timer and calls callback
function after delay
milliseconds.
The callback
function will be executed only once. If you’re looking for repeated execution, you should use setInterval.