Skip to content

Instantly share code, notes, and snippets.

@rik
Created September 1, 2017 14:30
Show Gist options
  • Save rik/56638f4efdd808c9af0e1a43b297d1a2 to your computer and use it in GitHub Desktop.
Save rik/56638f4efdd808c9af0e1a43b297d1a2 to your computer and use it in GitHub Desktop.

Revisions

  1. rik created this gist Sep 1, 2017.
    20 changes: 20 additions & 0 deletions simulateTyping.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    async function nextFrame() {
    return new Promise((resolve) => {
    requestAnimationFrame(resolve)
    })
    }
    async function randomDelay(min, max) {
    const delay = Math.random() * (max - min) + min;
    const startTime = performance.now()

    while (performance.now() - startTime < delay) {
    await nextFrame()
    }
    }

    async function simulateTyping({string, target, min = 10, max = 80}) {
    for (const letter of string) {
    target.insertAdjacentText("beforeend", letter)
    await randomDelay(min, max)
    }
    }