Sleep certain seconds with setTimeout() method - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window setTimeout

Description

Sleep certain seconds with setTimeout() method

Demo Code

ResultView the demo in separate window

<html>
   <head></head>
   <body> 
      <script>
var d = 120;/*w  w w.j  a  v  a  2 s. com*/
var start = new Date ();
document.write(start + " : sleeping for " + d + " seconds");

      </script> 
      <div id="result">
         <div> 
            <script>
function cb() {
  var now = new Date ();
  document.getElementById("result").innerHTML = "" + Date () + " : callback fired after " + (now.getTime() - start.getTime()) / 1000 + " seconds";
};
window.setTimeout(cb, d * 1000);

            </script>  
         </div>
      </div>
   </body>
</html>

Related Tutorials