Dot dotdot dotdotdot as loading?

前端 未结 8 2276
借酒劲吻你
借酒劲吻你 2021-01-31 07:43

I wanna create some loading dots, like this:

At 0000 miliseconds the span content is: .

At 0100 miliseconds the span content is: ..

8条回答
  •  独厮守ぢ
    2021-01-31 08:21

    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.

提交回复
热议问题