Perform some action after particular interval of time in JavaScript.

Aman Sharma
0
Some time we want to perform some action after particular interval of time. Ex. redirect to other page after 5 sec .  This can be done by using javaScript.

 In this article we will use JavaScript to redirect to  other page after particular interval of time.

Javascript Code to redirect after 5 sec :

<script type="text/javascript">

    function Redirect() {
        var sec = 5;
        var dvCount = document.getElementById("dvCount");
        var lblCount = document.getElementById("lblCount");
        dvCount.style.display = "block";
        lblCount.innerHTML = sec ;
        setInterval(function () {
            sec --;
            lblCount.innerHTML = sec ;
            if (sec  == 0) {
                dvCount.style.display = "none";
                window.location = "http://www.google.com/";
            }
        }, 1000);
    }
</script>



Sample Code to Redirect after 5 sec :

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <fieldset style="width:400px;"><legend><strong>Redirect to other Page After 5 Second Using JavaScript</strong></legend>
      <br />  Click here to redirect Page&nbsp;&nbsp;&nbsp;
<input type="button" value="Redirect" onclick="Redirect()" />
<br />
<br />
<div id="dvCount" style = "display:none">
You will be redirected after <span id = "lblCount"></span>&nbsp;sec .
</div></fieldset>
<script type="text/javascript">

    function Redirect() {
        var sec = 5;
        var dvCount = document.getElementById("dvCount");
        var lblCount = document.getElementById("lblCount");
        dvCount.style.display = "block";
        lblCount.innerHTML = sec ;
        setInterval(function () {
            sec --;
            lblCount.innerHTML = sec ;
            if (sec  == 0) {
                dvCount.style.display = "none";
                window.location = "http://www.google.com/";
            }
        }, 1000);
    }
</script>

</body>

</html>


Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !