There is an input with id = "tagname" . Now, after entering each letter, an ajax request is executed.

 $("#tagname").bind('input',function(e){ $.ajax({ // Тело ajax запроса }); }); 

How can I delay sending in 2s? I tried this:

 setTimeout(function() { $.ajax({ // Тело ajax запроса }); },2000); 

The delay is added, but after it several ajax requests are executed (depending on the number of characters entered). And you need to run one request at the end of the input.

Ie, after each pressed button, the counter should be reset, if during the 2sec another symbol was entered.

    1 answer 1

     var timerId; $("#tagname").bind('input',function(e){ clearTimeout(timerId); timerId = setTimeout(function() { $.ajax({ // Тело ajax запроса }); },2000); });