• 每隔一段事件返回字符串中的一个单词,类似打字机效果

    参数

    • words: string
    • delay: number
    • 可选callback: ((word: string, index: number, words: string) => false | void)
        • (word, index, words): false | void
        • 参数

          • word: string
          • index: number
          • words: string

          返回 false | void

    返回 ReturnType<typeof polling>

    const s = 'hello world';

    const receive: string[] = [];
    const diff: number[] = [];

    let lastNow = Date.now();
    const { promise } = oneByOne(s, 10, (w, index) => {
    receive.push(w);
    const now = Date.now();
    diff.push(now - lastNow);
    lastNow = now;
    });
    await promise;

    receive; // equals s.split('')
    diff[0]; // 0

    const _avg = avg(diff.slice(1));
    // 虽然是间隔10秒,但由于js的setTimeout并不精准,所以会有波动
    10 <= _avg && _avg <= 11; // true