I want to trigger an ajax request when the user has finished typing in a text box. I don\'t want it to run the function on every time the user types a letter because that wo
Wow, even 3 comments are pretty correct!
Empty input is not a reason to skip function call, e.g. I remove waste parameter from url before redirect
.on ('input', function() { ... });
should be used to trigger keyup
, paste
and change
events
definitely .val()
or .value
must be used
You can use $(this)
inside event function instead of #id
to work with multiple inputs
(my decision) I use anonymous function instead of doneTyping
in setTimeout
to easily access $(this)
from n.4, but you need to save it first like var $currentInput = $(this);
EDIT I see that some people don't understand directions without the possibility to copy-paste ready code. Here you're
var typingTimer;
// 2
$("#myinput").on('input', function () {
// 4 3
var input = $(this).val();
clearTimeout(typingTimer);
// 5
typingTimer = setTimeout(function() {
// do something with input
alert(input);
}, 5000);
});