nazerke
7/23/2015 - 2:00 PM

Solution to closure confusion. Lets each function in setTimeout to get its own i

Solution to closure confusion. Lets each function in setTimeout to get its own i

<!DOCTYPE html>
<html>
  <head>
   <script>

for (var i=1; i<=5; i++) {
	(function(i){
		setTimeout(function(){
			console.log("i: " + i);
		},i*1000);
	})(i);
}

</script>
  </head>
  <body>
    <!-- page content -->
  </body>
</html>