How to implement Ajax delay sending and send Ajax requests after idle


Many times, we will encounter functions such as keyword search

Our first idea is to bind the Keyup event of the response, and then request an Ajax to return the data processing of the response

However, at this time, if you simply bind the Keyup event. So, each keystroke will send an Ajax request, which is obviously not what we want.

We hope to send Ajax requests when we are free:

Therefore, we set the user to send the Ajax request after 1s without hitting the keyboard

Copy codeThe code is as follows:
var timeout;
onkeyup = function() {
timeout = setTimeout(function() {
Ajax request
}, 1000);

Of course, another idea can be used:

Block outgoing Ajax requests:

Copy codeThe code is as follows:
if (request) {
request = $.ajax( )

However, this Ajax request is best blocked before AJAX is issued