Run javascript function when user finishes typing instead of on key up?

Run javascript function when user finishes typing instead of on key up?

by Cong Nguyen Trong

HTML

<input type="text" id="myInput">

CSS

body {
    min-height: 200px;
    background: #eaeaea;
}

JavaScript

//setup before functions
let typingTimer; //timer identifier
let doneTypingInterval = 5000; //time in ms (5 seconds)
let myInput = document.getElementById('myInput');

//on keyup, start the countdown
myInput.addEventListener('keyup', () => {
    clearTimeout(typingTimer);
    if (myInput.value) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
});

//user is "finished typing," do something
function doneTyping() {
    //do something
    alert('With Vanila Js');
}