I have the following pattern to repeat an animation (itself consisting of x frames), t times:
sprite.prototype.loop = function(t,animation_name,frame_delay) {
if(t > 0) {
function next_run() {
this.loop(t-1,animation_name,frame_delay);
}
this.run(animation_name,frame_delay,next_run);
}
};
sprite.prototype.run = function(animation_name,frame_delay,frame_count,callback) {
frame_count ||= 0;
var s = this;
if(frame_count < this.paths[animation_name].length) { // x frames
setTimeout( function () {
s.blit(s.paths[animation_name][frame_count]);
s.run(animation_name,frame_delay,frame_count+1);
}, frame_delay );
}
} else {
if(!!callback) callback();
}
super_mario.loop(10000,'mushroom_death',40);
Clearly if x*t is larger than the maximum stack depth, a stack overflow will occur.
Question: Can this pattern be extended to the case of running an animation an infinite number of times, or is there a more correct way to do the infinite loop case?