I am trying to create a solar sistem in jQuery. I have 5 image planets that move on an ellipse path. When a planet reach the start point it should start again. I can’t make a while because there are 5 planets that must be moved and the browser will be blocked. I must call the function once again after a time (like 8 sec to make a cicle). The planets move slow after a period of time (a few minutes). There is a better way to do this ?
$(window).load(function() {
solar(10, 1, "#i1", getDim($("#i1")));
solar(200, 1, "#i2", getDim($("#i2")));
solar(400, 1, "#i3", getDim($("#i3")));
solar(400, 0, "#i4", getDim($("#i4")));
solar(200, 0, "#i5", getDim($("#i5")));
});
function solar(start, dir, id, dim) {
var nr = 0;
var i = start;
while (nr != 2) {
if (dir == 1) {
i += step;
if (Math.abs(i - 2*aa) < step)
dir = 1- dir;
} else {
i -= step;
if (Math.abs(i) < step)
dir = 1- dir;
}
if (Math.abs(i - start) < step) {
nr++;
}
p = getElipsePoint(p, dim);
$(id).animate(
{"left": p.x, "top": p.y},
{ duration:1 }
);
}
window.setTimeout(function() { solar(start, dir, id, dim) }, 8000);
}
You can determine when the animation has finished using a callback. Something like this: