Javascript examples for Browser Object Model:Window setTimeout
Make a function asynchronous functions with setTimeout(, 0)
<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>