在用戶輸入後1秒後執行操做。(請求服務器實現自動補全 或者別的操做) 服務器
主要不想每當用戶有輸入就向服務器提交請求。 less
<script> //request the server to get data function logs(){ $('#lable').text($('#t').val()); }; $(function(){ var time= (new Date()).getTime(); var req = setTimeout("logs()",1000); $('#t').keyup(function(){ var t = (new Date()).getTime(); var _t = time; time= (new Date()).getTime(); //if the time during 2 keyup is less than 1s, concel the last request. if(t-_t <1000) { clearTimeout(req); } // send a request req = setTimeout("logs()",1000); }) }) </script>