Window setTimeout() Method - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window setTimeout

Description

The setTimeout() method calls a function or evaluates an expression after a number of milliseconds.

Parameter Values

Parameter Description
functionRequired. The function that will be executed
millisecondsOptional. milliseconds to wait before executing the code. If omitted, the value 0 is used
param1, param2, ... Optional. Additional parameters to pass to the function

Return Value:

A Number, representing the ID value of the timer that is set.

Use this value with the clearTimeout() method to cancel the timer

The following code shows how to Display an alert box after 3 seconds (3000 milliseconds):

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<button onclick="openWin()">Open "myWindow"</button>

<script>
function openWin() {/*from  w w w .j  a v a2 s .  com*/
    var myWindow = window.open("", "myWindow", "width=200, height=100");
    myWindow.document.write("<p>This is 'myWindow'</p>");
    setTimeout(function(){ myWindow.close() }, 3000);
}

</script>

</body>
</html>

Related Tutorials