One possibility is to cut out the array and use a linked list.
Make each frame an object that points to the next object. Then the latter points to the first. Then all you have to do is reference the next object.
var curr = first; // reference to first frame object setInterval(function() { // process image curr.image.doSomething(); // proceed to next curr = curr.next; }, 1000);
There are no counters to obstruct this path.
Setting up a linked list is usually quite simple and can be done with a little modification of the current code that sets up the array.
var first = new Frame(); // This is your entry point var current = first; // This holds the current frame during setup for (var i = 0; i < totalFrames; i++) { current.next = new Frame(); // Make the current reference a new frame current = current.next; // Make the new frame current } current.next = first; // circular reference back to the first. function Frame() { // set up this frame }
Then first is your starting point.
Or the link can be made in the Frame constructor.
var first = new Frame(null); var current = first; for (var i = 0; i < totalFrames; i++) { current = new Frame(current); } current.next = first; function Frame(currFrame) {
user1106925
source share