I do a search on the site. There is an input in which the search string is entered. If I send a request to the server with each click of a key, then the results start to flicker and form a lot of requests to the server. I need to send the request to the server only after 1s as the person finishes typing. I think that the person will finish typing if the field value does not change within 1 second

That's what's left after an hour of attempts)

Or you can do it using jquery ui, but when you click on the keyboard button, a request should go to the server and see the result. I looked and didn’t notice this in jquery autocomplite, he chooses already from some data set ((

var input = $('input'); var begin=0; var stop = new Date().getTime(); input.keyup(function(e){ var value = this.value; $('div').children('span').remove(); console.log('----------------keyup----------'); console.log(begin); console.log(stop); console.log(value); if(value.length>=3){ send(); } function send(){ if(begin===0){ setTimeout(function(){ console.log('--begin = 0--'); send(); },1000); } begin = new Date().getTime()-stop;//прошедшее время console.log(begin); if(begin<1000){ console.log('надо ещё подождать'); setTimeout(function(){ console.log('== callback =='); send(); },1000-begin); }else{ console.log('пора отсылать'); $('div').append('<span>Text</span>'); stop = new Date().getTime(); } } }); 
 <!DOCTYPE html> <html> <head> <script src="https://code.jquery.com/jquery-2.1.4.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <input type="text"> <div></div> </body> </html> 

    1 answer 1

    If I understand correctly what is needed, then the delay function needs to be done

     var delay = (function(){ var currentTime = 0; return function(callback, ms){ clearTimeout (currentTime); currentTime = setTimeout(callback, ms); }; })(); $(input).keyup(function(){ delay(function(){ console.log('time over'); }, 1000 ); }) 
    • It is necessary that the search results (which are shown using the function) are displayed only after 1c as the person has finished entering data in the field. To avoid the flicker of the results. - WebWorkDeveloper
    • Well, with the help of such a delay, the request (and all other actions) will be carried out in a couple of seconds after you have stopped entering anything into the input. - Moonvvell
    • Aah .. I understood everything)) Thank you) - WebWorkDeveloper