JavaScript延时执行function 1.当用户输入搜索条件,即打字的时候起开始计时,到停止输入结束,1秒后执行查询function2.如果1秒内再有输入动作,再重新计时JavaScript如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <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"> 你可以用onchange时间监听用户输入,然后在onchange上再延时setTimeout(function(){},1000) onchange方法不行,当值改变的时候还需要鼠标变化焦点!! 试试这个<!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> <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"> ie6 7的iframe的bug 请问关于判断页面编码形式 迟到的漂亮下雪效果 急求:js中注销Session问题 两个列表框联动,如何限制个数? 如何用JavaScript在弹出新窗口是调用ASCII码使电脑蜂鸣器发声 请教DOCUMENT.WRITE 为什么VBS函数得不到JS传过去的值? IE浏览器的问题 请教,jQuery中的一个正则表达式 Ext.form.combox 拓展 求助。。。。 js从服务器到客户端错误
<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">
<!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>
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">