The last parameter of "animate" is "complete". This is a function you can run when the animation is complete - i.e. exactly what you ask for. http://api.jquery.com/animate/
You can't exactly say "when both are finished" (unless you set a variable=0, on complete: variable=variable+1, if variable=2 then both completed - but that has small risk of failure) so trust that they'll both end after the second and set the 'complete" on only one of the animates. Or add a fractional gap (say 0.1 of a second) and run with that.
So one method:
$("a").live("click", function(){
$(".circle1").animate({
marginLeft: "-=1000px"
}, 1000)
$(".circle2").animate({
marginLeft: "+=1000px"}, 1000, function() {
setTimeout(function() {alert('both done'); }, 100 );
});
And long winded method with fractional risk of failure:
$("a").live("click", function(){
var comp = 0;
$(".circle1").animate({
marginLeft: "-=1000px"
}, 1000, function() {comp++; if (comp==2) { alert('both done'); } })
$(".circle2").animate({
marginLeft: "+=1000px"
}, 1000, function() {comp++; if (comp==2) { alert('both done'); } })
});
Edit
After comment below, I've added all the necessary bits, corrected the "timeout" function etc (the above were samples to give an idea of thining). Here are test scripts with tweaks including using "on" (not "live"), cancelling the click and adding the loader.
<html>
<head>
<title>Test</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script>
$().ready( function() {
$("a").live("click", function(){
$(".circle1").animate({
marginLeft: "-=1000px"
}, 1000);
$(".circle2").animate({
marginLeft: "+=1000px"
}, 1000, function() { setTimeout(function() {alert('both done'); }, 100 );
});
return false;
});
});
</script>
</head>
<body>
<div id="counter"></div>
<a href="#">Click Me</a>
<div class="circle1"></div>
<div class="circle2"></div>
</body>
</html>
.
<html>
<head>
<title>Test</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script>
$().ready( function() {
$("a").on("click", function(){
var comp = 0;
$(".circle1").animate({
marginLeft: "-=1000px"
}, 1000, function() {comp++; if (comp==2) { alert('both done'); } });
$(".circle2").animate({
marginLeft: "+=1000px"
}, 1000, function() {comp++; if (comp==2) { alert('both done'); } });
return false;
});
});
</script>
</head>
<body>
<div id="counter"></div>
<a href="#">Click Me</a>
<div class="circle1"></div>
<div class="circle2"></div>
</body>
</html>
.