How To Use Window SetTimeout Method In JavaScript

admin_img Posted By Bajarangi soft , Posted On 30-09-2020

In Java script we can use setTimeout method which will calls a function or evaluates an expression after a specified number of milliseconds.so today we are going to discuss about SetTimeout Method In JavaScript

How To Use Window SetTimeout Method In JavaScript

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

1000 ms = 1 second.

The function is only executed once. If you need to repeat execution, use the setInterval() method.

 Use the clearTimeout() method to prevent the function from running.

Syntax and Usage

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

Parameter Values

Parameter           Description
function           Required. The function that will be executed
milliseconds       Optional. The number of milliseconds to wait before executing the code. If omitted, the value 0 is used
param1, param2, ...    Optional. Additional parameters to pass to the function (Not supported in IE9 and earlier)

Example(1)
<button class="btn btn-success" onclick="set_time()">Click it</button>

<script>
    function set_time() {
        setTimeout(function(){ alert("Hello"); }, 1000);
    }
</script>

In above example when you click button you get alert message after 3 seconds.

Example(2)

<button class="btn btn-success" onclick="get_time()">Click it</button>

<script>
    var Var;

    function get_time() {
        Var = setTimeout(alertFunc, 3000);
    }

    function alertFunc() {
        alert("Hello!");
    }
</script>

In above example when you click button you get alert message after 3 seconds.

Example(3)
<button class="btn btn-success" onclick="timedText()">Set time</button><br><br><br>

<div class="col-md-6">
    <input class="form-control" type="text" id="txt">
</div>
<script>
    function timedText() {
        var 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);
    }
   
</script>

In above example when you click on the button below. The input field will tell you when two, four, and six seconds have passed.

Example(4)
<button class="btn btn-success" onclick="openmin()">Open "Window"</button><br><br><br>
<script>
    function openmin() {
        var myWindow = window.open("", "myWindow", "width=200, height=100");
        myWindow.document.write("<p>am new mini window</p>");
        setTimeout(function(){ myWindow.close() }, 10000);
    }

</script>

In above example when you click the button to open a new window and close the window after three seconds (3000 milliseconds).

Complete Code For Window SetTimeout Method In JavaScript
<!DOCTYPE html>
<html>
<head>
    <title>How To Use Window SetTimeout Method In JavaScript</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<style>
    h1 {
        color: red;

    }

</style>

<body>
<div class="container">
    <div class="text-center">
        <h1>Use Window SetTimeout Method In JavaScript</h1>
    </div>
    <br>
   <div class="well">

       <button class="btn btn-info" onclick="set_time()">Set time</button><br><br><br>
       <button class="btn btn-primary" onclick="get_time()">Set time</button><br><br><br>
       <button class="btn btn-success" onclick="timedText()">Set time</button><br><br><br>

       <div class="col-md-6">
           <input class="form-control" type="text" id="txt">
       </div>
       <br><br><br>
       <button class="btn btn-success" onclick="openmin()">Open "Window"</button><br><br><br>
   </div>
</body>
</html>
<script>
    function set_time() {
        setTimeout(function(){ alert("Hello"); }, 1000);
    }
    var Var;

    function get_time() {
        Var = setTimeout(alertFunc, 3000);
    }

    function alertFunc() {
        alert("Hello!");
    }
    function timedText() {
        var 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);
    }
    function openmin() {
        var myWindow = window.open("", "myWindow", "width=200, height=100");
        myWindow.document.write("<p>am new mini window</p>");
        setTimeout(function(){ myWindow.close() }, 10000);
    }

</script>

Related Post