Is it possible to make an object that has its own setInterval();
I have this code here http://jsfiddle.net/mLHJr/1/ with a global setInterval. I want to have multiple instances of the circle object and each own to have its own interval with different framerate.
Is that possible?
Thanks in advance
There are a variety of approaches. You can use one timer and have it call an update function for each object in an array or collection. Or you can create separate objects that call their own update function and have their own timer.
The first option is usually more efficient.
You have another issue though – if each circle has its own timer, you can’t just re-draw the entire canvas each time as you are doing now. If you do, then each time it re-draws one ball it will delete the others so it will flicker between them. You can fix that by having the ball delete only itself before redrawing itself in the new location – but then you have to deal with overlapping balls.
So the best approach is one timer that re-draws all the balls each time in sequence so the ones “on top” always overlap the ones underneath.