Just add this line to the end of the loop:
i = (i === 3) ? 0 : i;
This is simply an abbreviation for the word "if i is 3, set it to zero, otherwise leave it as it is." This should start the cycle again until you set the exit condition.
EDIT : go on, I really didn't look like you added . (sorry, I can not get jsFiddle to run anything at the moment)! If you used i reset as above, you really need to set the number of characters . equal to i with each iteration.
EDIT 2 . Once again, looking at this, you even need to take i in close to get its value at the time the setTimeout declaration otherwise you will get any value when setTimeout executed , which is unpredictable. In principle, do not use this solution - use Jeff's !;)
Chris francis
source share