6

I just encountered a very weird issue (I fixed it though) but I wanted to know why did it happen in the first place:

function stuffAppear() {
    var i;
    for (i = 0; i < speech.length; i++) {
        apperance(i);
    }
}
function apperance(i) {
    var x = speech[i];
    setTimeout(function() {$(speech[i]).fadeIn(1000); console.log(i);}, 1000 + i * 1500);
    console.log(speech[i]);
}

The console log shows "#yo0" then "#ma0b" (which is the required) but at the same time, they never faded in

I played around with the code until I reached this:

function stuffAppear() {
    var i;
    for (i = 0; i < speech.length; i++) {
        apperance(i);
    }
}
function apperance(i) {
    var x = speech[i];
    setTimeout(function() {$(x).fadeIn(1000); console.log(i);}, 1000 + i * 1500);
}

And that did the trick, but I don't know why the first code didn't work. Can someone explain that to me, please? And thank you!

Mathspy
  • 172
  • 1
  • 2
  • 10
  • I actually checked it out. And changed my code according to it (my code originally was a setTimeout inside the For Loop that calls the function instead of a For Loop that calls a function which inside it is a setTimeout – Mathspy Apr 06 '14 at 08:37
  • Fascinating. It looks like it should have worked as you did pass i originally. Can you setup a JSFiddle with your HTML? @undefined: it is not the same as that other question, as he *was* passing the index as a local var. – iCollect.it Ltd Apr 06 '14 at 08:41
  • http://jsfiddle.net/2XhqH/ speech is defined as a global array and strings are being pushed to it – Mathspy Apr 06 '14 at 08:53
  • http://jsfiddle.net/2XhqH/1/ my apologises I used the HTML only because TrueBlueAussie said that he wanted to see the HTML (also the code is big and divided between lots of files because I am working on creating a simple game engine) (P.S: This code is used only for demonstrating, it doesn't do it this way in the real code) – Mathspy Apr 06 '14 at 09:01
  • 1
    BTW: Please correct `appearance` too - spelling is important in code too (especially if you are going to publish it on SO) :) – iCollect.it Ltd Apr 06 '14 at 09:17

1 Answers1

5

In a JSFiddle both versions work fine (and the same):

First: http://jsfiddle.net/TrueBlueAussie/Bkz55/3/

var speech = ["#yo0", "#ma0b", "#blah"];

function stuffAppear() {
    var i;
    for (i = 0; i < speech.length; i++) {
        apperance(i);
    }
}
function apperance(i) {
    var x = speech[i];
    setTimeout(function() {$(speech[i]).fadeIn(1000); console.log(i);}, 1000 + i * 1500);
    console.log(speech[i]);  // <<< THIS WOULD OCCUR IMMEDIATELY
}

Second: http://jsfiddle.net/TrueBlueAussie/Bkz55/4/

var speech = ["#yo0", "#ma0b", "#blah"];

function stuffAppear() {
    var i;
    for (i = 0; i < speech.length; i++) {
        apperance(i);
    }
}
function apperance(i) {
    var x = speech[i];
    setTimeout(function() {$(x).fadeIn(1000); console.log(i);}, 1000 + i * 1500);
}

So I suspect what you are seeing is a side effect of your other code (not shown).

The only odd thing is you were logging in the first version twice (once outside the setTimeout which would display at the start - as you mentioned)

Follow up:

Having now seen the real code, the cause was changing of the speech array during the timeouts. When the timeout function was finally hit the speech array was empty!

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202