Skip to content

Instantly share code, notes, and snippets.

@nuxodin
Last active July 1, 2022 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuxodin/72d7141f79a51d08a8914c8c883719f1 to your computer and use it in GitHub Desktop.
Save nuxodin/72d7141f79a51d08a8914c8c883719f1 to your computer and use it in GitHub Desktop.
track words using a delay to clean
// 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