0

I have this function:

    function animateImage() {
        for (i = 1; i < 31; i++) {

            top = (i * loadingHeight) + 'px';

            $(loadingDiv).animate({
                backgroundPosition: '0 -' + top
            }, 0).delay(50);

        }
    }

I want to infinite repeat/loop.

It is possible? how to do it?

4
  • 1
    Why are you making an instantaneous animation? Commented Nov 14, 2010 at 15:04
  • I think you are misunderstanding something fundamental about the syntax of animate, because it's completely unclear from your code what you are attempting to do. Can you please describe, in words, what you want the animation to look like? Commented Nov 14, 2010 at 15:15
  • @Ben Lee: I'm animating a background position to move 'down' 31 times every 50 ms. loadingHeight is a number. It creates an animated png image, instead of using an animated gif (freeze in IE) Commented Nov 14, 2010 at 15:29
  • 1
    @Jonathon, I'm not following exactly. What is the difference between moving down a loadingHeight every 50ms for 31 times, versus simply moving down 31*loadingHeight over the course of 31*50ms? Commented Nov 14, 2010 at 15:32

3 Answers 3

4

You can call animate in its completion callback.

For example:

function runAnimation() {
    $(loadingDiv).animate({
        backgroundPosition: '0 -' + top
    }, runAnimation)
}
Sign up to request clarification or add additional context in comments.

5 Comments

This will loop only the animation, I need to loop all the function.
@Jonathan: You need to write additional code in the function.
It wont work. The loop is inside the function but the animate() callback will call it on the first loop. So it will never loop or it will create some weird behavior...
@Jonathan: You need to change the function. The function will now be called separately for each iteration; you'll need global variables.
@Jonathan - you can do what you want with a simple change, I added an answer below to show how to queue up your function.
2

After queuing your 30 50ms background changes, you can just queue the function to run again, like this:

function animateImage() {
    var div = $(loadingDiv);
    for (i = 1; i < 31; i++) {
        var top = (i * loadingHeight) + 'px';
        div.animate({ backgroundPosition: '0 -' + top }, 0).delay(50);
    }
    div.queue(function(n) { animateImage(); n(); })
}

What this does is use .queue() to call animateImage once more after all of the other queued animations run, then it'll queue up 30 more animations, etc. The other change to cache $(loadingDiv) is just an optimization, it's a independent of the loop.

Comments

1

If you just want it to animate such that it moves loadingHeight pixels up every 50 milliseconds for a total of 31 loadingHeights, and then repeats back at the starting position, then you can do that in a single animation call, and then repeat that single call, like this:

(function animateImage(steps) {
    $(loadingDiv).css('backgroundPosition', '0 0');
    $(loadingDiv).animate(
        { backgroundPosition: '0 -' + (loadingHeight * steps) + 'px' },
        { duration: 50 * steps, complete: function() { animateImage(steps); } }
    );
})(31);

The general way to do an infinite or indefinite loop in javascript is to define a function that calls itself immediately and then periodically, like this:

(function someName() {
  // do some stuff
  // then after some delay (setTimeout or animate delay, etc...), call:
  someName();
})();

2 Comments

Looks good. There is a syntax error: Remove ; after 'px'. But how I call this function?
@Jonathan, thanks for the syntax error catch. It calls itself exactly as I coded it. The form (function name() {})(); is a function that is defined and called in one step. It is shorthand for function name() {}; name();

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.