How To Use Window SetInterval Method In JavaScript

admin_img Posted By Bajarangi soft , Posted On 01-10-2020

In Java Script we can use setInterval() method which calls a function or evaluates an expression at specified intervals (in milliseconds). so today we are going to discuss how to use setInterval() method in java script

How To Use Window SetInterval Method In JavaScript

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.

The ID value returned by setInterval() is used as the parameter for the clearInterval() method.

 1000 ms = 1 second.

To execute a function only once, after a specified number of milliseconds, use the setTimeout() method.

Syntax and Usage

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

Parameter Values

Parameter           Description
function           Required. The function that will be executed
milliseconds       Required. The intervals (in milliseconds) on how often to execute the code. If the value is less than 10, the value 10 is used
param1, param2, ...    Optional. Additional parameters to pass to the function (Not supported in IE9 and earlier)
 

Return Value:  A Number, representing the ID value of the timer that is set. Use this value with the clearInterval() method to cancel the timer

Example(1)

<button class="btn btn-success" onclick="get_alert()">click it</button>

<script>
    function get_alert() {
        setInterval(function(){ alert("Hello World"); }, 3000);
    }
</script>

In above Example when i click the button to wait 3 seconds, then alert "Hello World". After clicking away the alert box, an new alert box will appear in 3 seconds. This goes on forever...

Example(2)
<button class="btn btn-success" onclick="get_alert()">Click it</button>
<script>
    var Var;
    function get_alert() {
        Var = setInterval(alertFunc, 3000);
    }
    function alertFunc() {
        alert("Hello World!");
    }
</script>

In above example when you click the button to wait 3 seconds, then alert "Hello World".After clicking away the alert box, an new alert box will appear in 3 seconds. This goes on forever...

Example(3)
<h2 id="demo1"></h2>

<script>
    var Var = setInterval(myTimer, 1000);

    function myTimer() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("demo1").innerHTML = t;
    }
</script>
In above example clock will starts.

Example(4)
<h2 id="demo1"></h2>

<button class="btn btn-success" onclick="myStopFunction()">Stop time</button>

<script>
    var Var = setInterval(myTimer, 1000);

    function myTimer() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("demo1").innerHTML = t;
    }

    function myStopFunction() {
        clearInterval(Var);
    }
</script>
In above example starts this clock when you click button clock will stop.

Complete Code for Window SetInterval Method In JavaScript
<!DOCTYPE html>
<html>
<head>
    <title>Use Window SetInterval 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">
    <br>
    <div class="text-center">
        <h1>Use Window SetInterval 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>

       <h2 id="demo1"></h2>
       <button class="btn btn-primary" onclick="StopFunction()">Stop time</button><br><br><br>
       <h2 id="demo2"></h2>
   </div>
</body>
</html>
<script>
    function set_time() {
        setInterval(function(){ alert("Hello world am from set_time function"); }, 3000);
    }

    var Var;

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

    function alertFunc() {
        alert("Hello world am from get_time function");
    }
    var myVar = setInterval(myTimer, 1000);

    function myTimer() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("demo1").innerHTML = t;
    }

    var Var2 = setInterval(myTimer2, 1000);

    function myTimer2() {
        var d = new Date();
        var t = d.toLocaleTimeString();
        document.getElementById("demo2").innerHTML = t;
    }

    function StopFunction() {
        clearInterval(Var2);
    }
</script>

 

Related Post