How to use JavaScript setTimeout function

If you are wanting to run some JavaScript once after a set time, you should use setTimeout.

setTimeout(function() {
    // JavaScript code executes here
}, 3000)

The first parameter of the setTimeout is the JavaScript function that will be executed once the delay is up. The second parameter is the amount of time to delay in milliseconds.

It is possible to pass the setTimeout function a string in the first parameter as the JavaScript to execute but this is a bad idea. Passing the JavaScript as a string means that it must go through eval() to be executed. eval() == evil. It’s also difficult to pass parameters to a function when using strings that must go through eval(). Passing an anonymous function instead of a string is also easier to read for developers.