Last active
May 6, 2020 22:08
-
-
Save marta-krzyk-dev/94daefc0ccf0b6f5dc307534168766ac to your computer and use it in GitHub Desktop.
ClearTimer pattern in JS6
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
const div3 = document.getElementById('div3'); | |
const para = div3.querySelector('p'); | |
const textarea = div3.querySelector('textarea'); | |
const paraText = "User is typing"; | |
let timer; | |
textarea.addEventListener("keyup", addText); | |
textarea.addEventListener("keydown", removeText); | |
function addText(e){ | |
para.innerText = paraText; | |
} | |
function removeText(e){ | |
clearTimeout(timer); | |
timer = setTimeout(() => { | |
para.innerText = ""; | |
}, 1000); | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
</head> | |
<body> | |
<div id="div3"> | |
<p>User is typing</p> | |
<textarea></textarea> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment