


Making our code much more clean and simple. The main advantage compared to the setTimeout method we explained before, is that we can re-use the delay function. If we combine the setTimeout function with promises, we can then create a more readable code and place our whole code inside the same (async) function. Note that Promises are part of the ES6 standard and won't work in some legacy browsers such as IE 11. Therefore, keep on reading! Wait using PromiseĪnother way to make an asynchronous wait with moder JavaScript is by using the Promise function. 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. let timeoutID setTimeout (function, delay in milliseconds, argument1, argument2.) Let's break down the syntax. Here is the syntax for the setTimeout () method. The setTimeout function takes two parameters. What is setTimeout () in JavaScript setTimeout () is a method that will execute a piece of code after the timer has finished running. 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. Also, setTimeout doesn’t work for some reason, so that’s also a no go.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. After a lot of Googling I found out that OpenHAB doesn’t support the current version of Javascript, which means you can’t do the suggestion in the top StackOverflow post here.

I’ve spent hours on this and finally figured it out, so I thought I’d post it here in case anybody else runs into the same issue.
