How can I make this a better recursive animated jQuery script?

The idea is to animate a cloud div and have it animated back and forth horizontally to infinity. This works, but unfortunately I think it is prone to memory leaks and user interface latency. Any advice would be appreciated, thanks.

function animateCloud() {
    $('#cloud').animate({ right: '+=500' }, { duration: 35000, easing: "linear", queue: true });
    animateOpposite();
}

function animateOpposite() {
    $('#cloud').animate({ right: '-=500' }, { duration: 35000, easing: "linear", queue: true });
    animateCloud();
}

$(document).ready(function() {
    animateCloud();
});
+3
source share
2 answers

I don't think your code is completely memory leak, but you can create a call shorter.

function animateCloud() {
    $('#cloud').animate({ right: '+=500' }, { duration: 35000, easing: "linear" })
               .animate({ right: '-=500' }, { duration: 35000, easing: "linear", complete: animateCloud });
}

example: http://www.jsfiddle.net/bh3f4/

+4
source
+1

Source: https://habr.com/ru/post/1785302/


All Articles