2

What is the problem with my script? When I execute it, the alert (line 2) gives me "100,200,300undefinedundefined", so is seems like 100,200,300 is interpreted as h1 when I would like it to be h1, h2 and h3 (with the commas).

function myanimation(h1,h2,h3) {

        alert(h1 + h2 + h3);
        $("#h1").animate({"left": h1});
        $("#h2").animate({"left": h2});
    }

    var moves = new Array()
    moves[1] = [100,200,300];
    moves[2] = [300,200,100];
    moves[3] = [-500,-300,0];

    var i = 1;

    function animatenow(){
        myanimation(moves[i]);
        i++;
    }

$('#launch').click(function() {
        setInterval(animatenow, 5000);
    });

4 Answers 4

6

You are passing an array into myanimation, which corresponds to your h1 parameter. You aren't passing h2 or h3, so those are undefined.

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

Comments

2

Adam, you are passing in an array object into H1, not separate variables

You probably want to change

myanimation(moves[i]);

to:

myanimation(moves[i][0],moves[i][1],moves[i][2]);

2 Comments

Another question: when I use i++ in my function, it doesn't work; it always sends moves[1] so how can I make the i++ scope be global instead of local?
@AdamStrudwick it's not doing that for me. Could there be some other code that might be resetting i?
2

You're passing an array to myanimation where it expects three parameters.

myanimation(moves[i]); where moves[1] = [100,200,300]

So h1 in your myanimation would be [100,200,300]

Change it to expect an array:

function myanimation(moves) {
    $("#h1").animate({"left": moves[1]}); // moves[1] is 100
    $("#h2").animate({"left": moves[2]}); // 200
}

Comments

0

Based on your codes, should the following codes like this?

function animatenow(){
    myanimation(moves[i][0], moves[i][1], moves[i][2]);
    i++;
}

1 Comment

Another question: when I use i++ in my function, it doesn't work; it always sends moves[1] so how can I make the i++ scope be global instead of local?

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.