1

I am trying to get jQuery to remove an element after it as run through a cycle of other functions, but following code block executes the remove() before it has run any of the for loop.

function waves(){
    for(i=0;i<=10;i++){
            wave(x);
    };
    $(x).remove();
}
5
  • 1
    What does Wave function do? Where is x defined. Can you post that? Commented Jun 20, 2011 at 14:21
  • this may be a really dumb question, but why do you have a semi-colon after the closing brace of the for-loop? Commented Jun 20, 2011 at 14:34
  • not sure, it doesn't cause an error. Commented Jun 20, 2011 at 14:37
  • It seems, that the wave function is some kind of async function. It's interesting to see the full code Commented Jun 20, 2011 at 14:51
  • @Alexander, this is just the way jQuery sequentially runs. Commented Jun 20, 2011 at 15:00

1 Answer 1

2

Add an if statement into your loop, that then calls your function.

function waves(){
    for(i=0;i<=10;i++){
            wave(x);
            if (i == 10) {
               callback();
            }
    };
}
function callback() {
    $(x).remove();
}
Sign up to request clarification or add additional context in comments.

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.