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>