LoginSignup
9
10

More than 5 years have passed since last update.

ループ処理で変数の値がおかしい。

Last updated at Posted at 2014-05-25

0,1,2になるところが、3が3回表示されてしまう。

for(var i = 0; i < 3; i++) {
  setTimeout(function(){ 
    alert(i);
  }, 1000);
}

これで解決。

for(var i = 0; i < 3; i++) {
  hoge(i);
}
function hoge(delay){
  setTimeout(function(){ 
    alert(delay);
  }, 1000);
}
9
10
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
10