Run a function after user stop typing

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Run a function after user stop typing



I got a textbox that once the user stops typing, I want to update the results(what ever they type will be applied to an array of data and will filter down anything that does match what they type).



Right now I am using onBlur but this of course will only activate after they leave the textbox.



Would it be better to do onChange and put a timer that gets cancelled if they continue to type?



Or is there a better way.



Not sure if it makes a difference but I am reactjs





what do you mean I want to update the results.. I honestly do not understand the scenario here. Can you please make it more clear?
– vsync
48 mins ago



I want to update the results





I think you want to use a debounced onChange event (see lodash for example).
– samsonthehero
47 mins ago





Maybe stackoverflow.com/q/23123138/125981
– Mark Schultheiss
39 mins ago





also a debounce function example in this answer stackoverflow.com/a/37284592/125981 and here stackoverflow.com/a/47876137/125981 if you want something simple
– Mark Schultheiss
35 mins ago





I have an answer to a similar question, to "run a function after a user has finished typing in a text field", stackoverflow.com/a/37494269/2430549
– HoldOffHunger
18 mins ago




2 Answers
2



Just use onkeyup event it will fire when user releases keyboard button.


document.getElementById('inputText').onkeyup = UpdateData()



This is how you would do it in vanilla javascript (without React)


var inputElm = document.querySelector('input');

inputElm.addEventListener('input', onInput);

function onInput()
var duration = 1000;
clearTimeout(inputElm._timer);
inputElm._timer = setTimeout(()=>
update(this.value);
, duration);


function update()
console.log('Do something')


<input>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS