Return to Snippet

Revision: 31104
at August 30, 2010 06:06 by mjsiemer


Initial Code
for (i=0;i<11;i++){
    //this will create 11 timers that will alert the value of 'i' at the time 
    //the timer was created, all to fire after 1 second
    //to change what gets alerted, change the "(i)" portion at the end
    //can be used with more complex functions by adding to what's after "return"
    //can pass in multiple variables by adding to function(x,y,z), putting those 
    //in proper places w/in the code, and then defining those variables at the end
    //ie, "(i,j,k)"
    setTimeout(function(x){return function(){alert(x)};}(i),1000);
}

Initial URL
http://codingforums.com/showpost.php?p=598389&postcount=5

Initial Description

                                

Initial Title
Settimeout with closure in for loop

Initial Tags
javascript

Initial Language
JavaScript