Read variable in function() with setTimeout(fn()) - Javascript Browser Object Model

Javascript examples for Browser Object Model:Window setTimeout

Description

Read variable in function() with setTimeout(fn())

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 ww  w  .  j a v  a 2s.c  o  m*/
for(var i=1; i<=3; i++){
    (function(num){
        setTimeout(function(){
            say("No:" + num);
        }, num * 1000);
    })(i);
}
function say(text){
    console.log(text);
}
    }

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

Related Tutorials