


This will very probably be ok in any circumstance, and the solution to it can be as simple as placing the rest of the code inside the timeout too, but it is not something that can serve us in every scenario.Īlso, if you want to keep on adding delays inside the same delayed function, we can easily run into what is known as the callback hell (and that's not pretty). This means that anything defined after it on the code will run before the delay. Notice how our setTimeout function runs asynchronously and that it won't stop the flow of our program. log ( "Executed before the delay, but after the 1st console.log" ) Now, this looks great and simple! However, here's the trick: console. In this case, we are using 1000ms, which is the equivalent of 1 second. One is the callback function that will get executed after a specific time, and the other is the number of milliseconds we want to delay the execution. The setTimeout function takes two parameters. One of the easiest way to achieve a 1 second delay is by using the setTimeout function that lives on the window global object.

Some are better than others and some should only be used in specific circumstances. There are quite a few ways to tell JavaScript to wait for 1 second. To force a 1 second pause or delay in JavaScript we can use the setTimeout function, but if we want a better solution we should use the Promise function.
