Спасибо еще раз. Сделал так:
var animate = function foo(animationVar) {
if (typeof (foo.inside) === "undefined" && typeof (foo.outside) === "undefined" {
foo.outside = 0;
foo.inside = 0;
}
foo.outside++;
...
foo.inside++;
if (foo.outside !== foo.inside) return;
window.setTimeout(function() {animate(animationvar + 1);}, 50);
}