JavaScript延时执行function

superman_zgd 2010-04-27 10:14:12
1.当用户输入搜索条件,即打字的时候起开始计时,到停止输入结束,1秒后执行查询function
2.如果1秒内再有输入动作,再重新计时

JavaScript如何实现?
...全文
305 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
come521 2010-12-07
  • 打赏
  • 举报
回复
客户端延时不好整
gnahshining 2010-04-27
  • 打赏
  • 举报
回复
<script type="text/javascript">
var s="";
var index = 0;
var i = 0;
document.onkeypress =function(){

setTimeout("setVal()",1000);
i++;

}

function setVal(){
index ++;
if(index==i) {
s = document.getElementById("txt1").value;
alert(s);
index = 0;
i=0;
}



}
</script>
<input type=text id="txt1">
打字员 2010-04-27
  • 打赏
  • 举报
回复
试试这个


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<script language="javascript">
function searchKeyWord(id) {
this.element = document.getElementById(id);
this.timeID = null;
this.init();
}
searchKeyWord.prototype = {
init: function() {
var _self = this;
this.element.onkeyup = function() {_self.start()};
this.element.onblur = function() {_self.end()};
},
start: function() {
this.resetTime();
var _self = this;
this.timeID = setTimeout(function(){_self.end()}, 1000);
},
end: function() {
if(this.timeID) {
this.resetTime();
this.searchIt();
}
},
resetTime: function() {
clearTimeout(this.timeID);
this.timeID = null;
},
searchIt: function() {
var _value = this.element.value.replace(/(^\s*)|(\s*$)/g, '');
document.getElementById('t').value = _value;
}
}
</script>
<body>
<input type="text" id='keyWord' />
<input type="text" id='t' />
<script language="javascript">
new searchKeyWord('keyWord');
</script>
</body>
</html>

jack_liu4Ye 2010-04-27
  • 打赏
  • 举报
回复
onchange方法不行,当值改变的时候还需要鼠标变化焦点!!
weibawoaini 2010-04-27
  • 打赏
  • 举报
回复
你可以用onchange时间监听用户输入,然后在onchange上再延时setTimeout(function(){},1000)
passself 2010-04-27
  • 打赏
  • 举报
回复

<script type="text/javascript">
var s="";
document.onkeypress =function(){
s+=String.fromCharCode(event.keyCode);
}

function setVal(){
document.getElementById("txt1").value=s;
s="";
}
setTimeout("setVal()",3000);
</script>
<input type=text id="txt1">

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧