Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i run the .animate function in jQuery forever?

$(this).css("left","100px");

function endless(){
    $(this).animate({
        left:'-=100px',
    },{
        easing: "linear",
    duration: 5000,
    complete: function() {
        $(this).css('left','100px');
    endless();
        }
    });
};
endless();

This is what I tried, but using this approach i can't get stuff moving. Im' using jQuery 1.3.2. Any Suggestions?

like image 877
Julian Weimer Avatar asked Jul 30 '09 12:07

Julian Weimer


People also ask

How can stop infinite loop in jQuery?

var timer = setTimeout(function(){ fadeItIn(); },5000); and then to stop the timer: clearTimeout(timer);

What is STOP () in jQuery?

The jQuery stop() method is used to stop an animation or effect before it is finished. The stop() method works for all jQuery effect functions, including sliding, fading and custom animations.

How do you animate in jQuery?

jQuery Animations - The animate() Method The jQuery animate() method is used to create custom animations. Syntax: $(selector). animate({params},speed,callback);

Can the animate () method be used to animate any CSS property?

The animate() method is typically used to animate numeric CSS properties, for example, width , height , margin , padding , opacity , top , left , etc. but the non-numeric properties such as color or background-color cannot be animated using the basic jQuery functionality. Note: Not all CSS properties are animatable.


1 Answers

You have the parameters to animate wrong. It doesn't take an options hash, just the actual options for easing, duration, and a callback. Also, you need to take care when using this. Better to pass it in as an argument to the endless function.

$(this).css("left","100px");

function endless(elem){
    $(elem).animate(
        { left:'-=100px' },
        "linear",
        5000,
        function() {
            $(elem).css('left','100px');
            endless(elem);
        }
     );
};
endless(this);
like image 124
tvanfosson Avatar answered Sep 30 '22 20:09

tvanfosson