I am using Turbolinks.visit
action, via $(document).on("input");
...
Html
<form id="mainSearch" method="get" autocomplete="off">
<input type="search" name="s" placeholder="Search" />
</form>
Javascript
$(document).off().on("input", "#mainSearch", function(e) {
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
var ser = $(this).serialize();
setTimeout(function(){
Turbolinks.visit(
homeurl+"/?"+ser,
{
change: ['content']
}
);
},110);
return false;
});
It is working right but event fired multiple request when hold down a key.
do how I take precautions to keep it pressed? .on("input"
Did you forget to cancel the previous timer?
var timer = null;
$(document).off().on("input", "#mainSearch", function(e) {
e.stopImmediatePropagation();
e.stopPropagation();
e.preventDefault();
var ser= $(this).serialize();
clearInterval(timer);
timer = setTimeout(function(){
Turbolinks.visit(
homeurl+"/?"+ser,
{
change: ['content']
}
);
},110);
return false;
});
Edit: Instead of re-inventing the wheel it's always a good choice to use existing solutions, $.debounce()
is what you are looking for.