Make a function asynchronous functions with setTimeout(, 0) - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window setTimeout

Description

Make a function asynchronous functions with setTimeout(, 0)

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from  w  ww .  j  a  v  a 2  s . c  o  m
var i = 0;
function foobar(){
    setTimeout(function(){ console.log(++i); }, 0);
    setTimeout(function(){ console.log(--i); }, 0);
}
foobar();
foobar();
foobar();
foobar();
foobar();
console.log('This will ALWAYS alert first');
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials