Last active
July 1, 2022 20:02
-
-
Save nuxodin/72d7141f79a51d08a8914c8c883719f1 to your computer and use it in GitHub Desktop.
track words using a delay to clean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// better a "WordObserver" that can better handle chanching targets? | |
let word = ''; | |
let wordClearTimeout = null; | |
let wordTarget = null; | |
addEventListener('keyup', e=>{ | |
const isChar = /^.$/u.test(e.key); | |
// if (wordTarget !== e.target) { // clear if target changes | |
// word = ''; | |
// wordTarget = e.target; | |
// } | |
if (e.key === 'Backspace') { | |
word = word.slice(0, -1); | |
} else if (!isChar) { | |
word = ''; | |
return; | |
} else { | |
word += e.key; | |
} | |
clearTimeout(wordClearTimeout); | |
wordClearTimeout = setTimeout(()=>{ | |
e.target.dispatchEvent(new CustomEvent('u1-key-word', {bubbles: true, detail: word})); | |
// dispatch here? | |
word = ''; | |
}, 800); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment