views:

591

answers:

1

At this url - http://thespacebetweenthewords.org/sandbox/

I am attempting to cycle through arrays of words with a jQuery FadeIn/FadeOut effect.

The array of words displayed doesn't seem to be starting at [0], though. I am perhaps missing something with the order in which the functions are executing.

If anyone in the community would like to comment on the code as it stands so far, insight is appreciated.

+1  A: 

You are correctly using a callback function with .fadeIn() to ensure that the word doesn't start to fade out until it's finished fading in. However, you haven't applied the same principle to iterating through the verbs. Your outer for loop will tear through all the verbs in no time, showing each one, and then patiently waiting until it's time to hide them (when, of course, only the most recent verb will still be showing by then).

You might try something like this:

function showWord(selector, words, wordTime, i) {
    if (words.length < i)
        return;
    var word = words[i];
    $(selector).html(word).fadeIn(wordTime, function() {
        $(selector).fadeOut(wordTime, function() {
            showWord(selector, words, wordTime, i + 1);
        });
    });
}
showWord("#verb-content", data.verbs, verbTime, 0);
VoteyDisciple
I have updated the code, here - http://thespacebetweenthewords.org/sandbox/The first string in each of the arrays is showing, but it appears as though the function is not getting called to fadeIn the next string in each of the arrays. I'm not sure what the best way is to pass values into the callback to showWord from fadeOut.
jerome
Indeed, I wrote that call with completely the wrong arguments. I've edited my version above and tested it successfully.
VoteyDisciple
Ha. I realized too that the function needed to be passed all its arguments and updated here - http://www.thespacebetweenthewords.orgI also adjusted it so that the words would infinitely loop.
jerome