-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtyping.js
More file actions
22 lines (19 loc) · 706 Bytes
/
Copy pathtyping.js
File metadata and controls
22 lines (19 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let currentAnimToken = 0;
async function animateText(el, newText, options = {}) {
const { deleteDelay = 5, typeDelay = 30 } = options;
const token = ++currentAnimToken; // unique ID for this run
// 1) Delete phase
let oldText = el.innerHTML;
for (let i = oldText.length; i >= 0; i--) {
// if a new animation started, abort this one
if (token !== currentAnimToken) return;
el.innerHTML = oldText.slice(0, i);
await new Promise((r) => setTimeout(r, deleteDelay));
}
// 2) Type phase
for (let i = 0; i <= newText.length; i++) {
if (token !== currentAnimToken) return;
el.innerHTML = newText.slice(0, i);
await new Promise((r) => setTimeout(r, typeDelay));
}
}