Home>

When a text box,The content inside remains unchanged for 1 second,It means that the user has no input within 1 second,It means that the user has completed the input,Wait for the data to return.So how does jquery realize that there is no input in 1 second,Learn through this article together

Monitoring for a period of time without input can be used for simple input completion

The problem of sending a request to the background to obtain data and displaying it to the foreground was implemented last time when an analog input was completedThe core criterion is the definition of input completion.

which is:How the user counts the input completed.

The criteria I use is,When a text box,The content inside remains unchanged for 1 second,It means that the user has no input within 1 second,It means that the user has completed the input,Wait for the data to return.So how does jquery realize that there is no input within 1 second?


<
!
doctype html>
<
html>

<
head>

<
script src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"><
/ script>

<
script>

var lasttime;

$(function () {
$('#text1'). keyup (function (e) {
lasttime=e.timestamp;

settimeout (function () {
if (lasttime-e.timestamp == 0) {
alert ("I didn't enter it in 1 second,Can send inquiry ");

}
}, 1000);

});

})
<
/ script>

<
/ head>

<
body>

<
input id="text1" type="text" value="" />
<
/ body>

<
/ html>

principle

This is a streamlined piece of code.The principle can be briefly described as follows:

Every time the user presses the keyboard,Both record the current time,Then judge the time after 1 second.

Since lasttime is a global variable,So when the user is typing,Lasttime is always changing,So use lasttime-e.timestamp again after one second,Only the last time e.timestamp is pressed will be 0.

  • Previous Implementation method of menu disappearing when jQuery clicks elsewhere
  • Next Selection of Java Classic Algorithm Summary (SelectionSort)