adsterra

Timing Event in Java Script |setTimeout() in JavaScript|


Timing Event in Java Script

Code:
<html lang="en">
<head>
    <title>Document</title>
    <script type="text/javascript">
     var id=0;
     var sec=0;
    function printMsg(){
        //document.getElementById("op").innerHTML="5 second have passed"
    document.getElementById("op").innerHTML=sec+" Seconds";
    sec++;
    }
    function start(){
        //id=window.setTimeout(printMsg,5000)
         id=window.setInterval(printMsg,1000);//work as timer
    }
    function stop(){
       //window.clearTimeout(id);
         window.clearInterval(id)
    }
    </script>
</head>
<body>
    <button onclick="start()" id="btnAdd">Start</button>
    <h2 id="op">Some text</h2>
    <button onclick="stop()" id="btnStop">Stop</button>
</body>
</html>

Post a Comment

0 Comments