I have this part of code:
$('#clientsfilter').on('input', function() {
var string = $('#clientsfilter').val();
if (string != "") {
//console.log("input is not empty");
var timeoutid = setTimeout(function() {
// some stuff
console.log("fired!");
}, 1500);
console.log("timeoutid is "+timeoutid);
}
else
{
// console.log("input is empty");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="clientsfilter" type="text" size="27">
When a user types something, it perform some actions after 1,5 seconds. But if a user will type one symbol after another too fass (time between symbols less than 1,5 seconds), the setTimeout() stuff will fire every time user adds symbol (or deletes). I want to prevent firing setTimeout function too many times, only if 1.5 seconds passed from last typing. I found information about
clearTimeout(timeoutid);
But how can I use it my situation?