Window setTimeout() method

Definition and Usage

setTimeout() The method calls the function after a certain number of milliseconds.

Note:1 second = 1000 milliseconds.

Hint

setTimeout() to execute only once.

If you need to repeat the execution, use setInterval().

Please use clearTimeout() method to prevent the function from starting.

To clear the timeout, use setTimeout() Returned id:

myTimeout = setTimeout(function, milliseconds);

Then you can stop the function by calling clearTimeout() method to stop the execution:

clearTimeout(myTimeout);

See also:

clearTimeout() method

setInterval() method

clearInterval() method

Example

Example 1

A greeting that waits for 5 seconds:

const myTimeout = setTimeout(myGreeting, 5000);

Try It Yourself

Example 2

Use clearTimeout(myTimeout) to prevent the execution of myGreeting:

const myTimeout = setTimeout(myGreeting, 5000);
function myStopFunction() {
  clearTimeout(myTimeout);
}

Try It Yourself

More examples are provided below the page.

Syntax

setTimeout(function, milliseconds, param1, param2, ...)

parameters

parameters description
function Required. The function to be executed.
milliseconds

Optional. The number of milliseconds to wait before execution.

Default value is 0.

param1, param2,...

Optional. Parameters passed to the function.

Not supported in IE9 and earlier versions.

return value

type description
number.

the timer ID.

Please associate this id with clearTimeout(idmethod used together to cancel the timer.

Browser support

is supported by all browsers setTimeout():

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Supports Supports Supports Supports Supports Supports

More examples

Example 3

Display a warning box after 3 seconds (3000 milliseconds):

let timeout;
function myFunction() {
  timeout = setTimeout(alertFunc, 3000);
}
function alertFunc() {
  alert("Hello!");
}

Try It Yourself

Example 4

Display timing text:

let x = document.getElementById("txt");
setTimeout(function(){ x.value = "2 seconds" }, 2000);
setTimeout(function(){ x.value = "4 seconds" }, 4000);
setTimeout(function(){ x.value = "6 seconds" }, 6000);

Try It Yourself

Example 5

Open a new window and close it after three seconds (3000 milliseconds):

const myWindow = window.open("", "", "width=200, height=100");
setTimeout(function() {myWindow.close()}, 3000);

Try It Yourself

Example 6

Continuous counting - but counting can be stopped:

function startCount()
function stopCount()

Try It Yourself

Example 7

Clock created using timer events:

function startTime() {
  const date = new Date();
  document.getElementById("txt").innerHTML = date.toLocaleTimeString();
  setTimeout(function() {startTime()}, 1000);
}

Try It Yourself

Example 8

Pass parameters to the function (does not work in IE9 and earlier versions):

setTimeout(myFunc, 2000, "param1", "param2");

Try It Yourself

Example 9

But if you use an anonymous function, it will work with all browsers:

setTimeout(function() {myFunc("param1", "param2")}, 2000);

Try It Yourself