3

I've been trying to make a script to change the text of a span.

Admittedly I'm not fantastic with jQuery and have found a script. I've edited i but i cannot get the script to loop and i don't know where to even start. Any code hints or links to relevant documentation would be greatly appreciated.

Here's the jQuery so far:

function change() {
        $('#msg').html(options.pop()).fadeIn(750).delay(2000).fadeOut(750, change);
};

var options = [
    "Red Bull",
    "Smoke",
    "Babes",
    "css",
    "batman"
].reverse(); 
change();

And on jsfiddle: http://jsfiddle.net/5s8y3/214/

3
  • Your fiddle is appear to be working. What exactly do you need? Commented Apr 8, 2014 at 13:17
  • @BasitSaeed, it's not looping. It just cycles once. Commented Apr 8, 2014 at 13:20
  • @BasitSaeed It stops on the last variable of the array :< Commented Apr 8, 2014 at 13:22

4 Answers 4

6

Stop popping off the array and use an iterator instead.

var messages = ["Red Bull", "Smoke", "Babes", "css", "batman"],
    i = 0;

(function change() {
    var msg = messages[i > messages.length - 1 ? (i = 0) : i++];
    $("#msg").html(msg).fadeIn(750).delay(2000).fadeOut(750, change);
})();

JSFiddle

Sign up to request clarification or add additional context in comments.

4 Comments

I don't think I've ever seen that syntax. Nice (I think).
It's just a named IIFE and a ternary, no magic, but it might seem a little "strange" at first ?
True. Here's an option if you want to randomize it jsfiddle.net/5s8y3/221
I guess it seems strange because it's not obvious that i=0 : i++ are returning i, not just setting it.
3

Just put a global variable:

var i = 0;

Now, add this to your change() function:

if (i < messages.length) {
    i++;
} else {
    i = 0;
}

This portion of code, will loop over your array and go back to the beginning when it has reached the end.

Then, change messages.pop() to messages[i].

Here is a JSFiddle.

Comments

2

The issue is that Array.pop removes the element from the array, so once it's gone through your list of messages once there's no more entries and it starts throwing errors. To get it to loop continuously, add the newly removed message to the front of the array:

function change() {
    var message = messages.pop();
    messages.unshift(messages);
    $('#msg').html(message).fadeIn(750).delay(2000).fadeOut(750, change);
};

Comments

1

pop() will remove the value from array. So, it works for one full cycle. After that array is empty. So, it displays blank value. actually it works in loop.

var messages = [
    "Red Bull",
    "Smoke",
    "Babes",
    "css",
    "batman"
].reverse();

var i = messages.length;
function change() {
        i=(i%messages.length);
        $('#msg').html(messages[i++]).fadeIn(750).delay(2000).fadeOut(750, change);

};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.