Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <button onclick="buggy()">Click Me Buggy!</button>
    <button onclick="fixed()">Click Me Fixed!</button>
  </body>
</html>
 
function buggy() {
  for ( var i = 0; i < 5; i++ ) {
    console.log("Delay: ", i*100);
    setTimeout(function() {
        console.log("i:", i);
    }, i * 100 );
  }
}
function fixed() {
  for ( var i = 0; i < 5; i++ ) {
    console.log("Delay: ", i*100);
    (function(safe_i) {
      setTimeout(function() {
        console.log("i:", safe_i);
      }, i * 100 );
    }) (i);
  }
}
Output 300px

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers