Create timer (setTimeout) array and use them one by one - Javascript Array Operation

Javascript examples for Array Operation:Array Element

Description

Create timer (setTimeout) array and use them one by one

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
var myTimer = [];/*from w w w . j  a  va2 s  .com*/
function myFunction () {
    myTimer.push(setTimeout(function(){console.log("Hello")},3000));
}
function myStopFunction () {
    clearTimeout(myTimer.pop());
}

      </script> 
   </head> 
   <body> 
      <button onclick="myFunction()">Try it</button> 
      <button onclick="myStopFunction()">Stop the alert</button>  
   </body>
</html>

Related Tutorials