In many cases, we will encounter the keyword search function.
Our first thought is to bind the response's keyup event, and then request an ajax response for processing the obtained keyword.
However, in this case, if you simply bind the keyup event. Therefore, an ajax request is sent every time you press the keyboard. This is obviously not what we want.
We hope to try to send ajax requests when we are idle:
Therefore, we set the user to send this ajax request one second after not hitting the keyboard.
Copy codeThe Code is as follows:
Var timeout;
Onkeyup = function (){
ClearTimeout (timeout );
Timeout = setTimeout (function (){
Ajax request
},1000 );
}
Of course, you can use another idea:
Block the ajax request:
Copy codeThe Code is as follows:
If (request ){
Request. abort ();
}
Request = $. ajax ()
But it is best to block this ajax request before ajax is sent.