I wanna create some loading dots, like this:
At 0000 miliseconds the span content is: .
At 0100 miliseconds the span content is: ..
In my mind, the easiest way is an if
/else
statement.
However, a bit math to calculate the dots-length and the famous Array.join
-hack to repeat the dot-char, should do the trick too.
function dotdotdot(cursor, times, string) {
return Array(times - Math.abs(cursor % (times * 2) - times) + 1).join(string);
}
var cursor = 0;
setInterval(function () {
document.body.innerHTML = dotdotdot(cursor++, 3, '.')
}, 100);
You could improve the readability a bit, by wrapping the "count-up-and-down"-part and "string-repetition" in separate functions.