javascript

Run a Javascript function after user finished writing in the input field

The code can be used to run a function when the user finished writing in javascript.

var type_timer;
var finished_writing_interval = 3000;
var my_input = document.getElementById("my_input");

//Start timeout when user start typing
my_input.addEventListener('keyup', function () {
  clearTimeout(type_timer);
  type_timer = setTimeout(finished_typing, finished_writing_interval);
});

//Clear timeout on key down event
my_input.addEventListener('keydown', function () {
  clearTimeout(type_timer);
});

//This function runs when user has finished writing in input
function finished_typing () {
    console.log("User finished typing");
    //Do something here ...
}
Was this helpful?