[英]Delay running a function for 3 seconds?
我希望将此功能延迟运行3秒钟:
<script type="text/javascript">
$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
.on('focus', function(){
var $this = $(this);
if($this.val() == '4/11/2013'){
$this.val('');
}
})
.on('blur', function(){
var $this = $(this);
if($this.val() == ''){
$this.val('4/11/2013');
}
});
</script>
我遇到的所有示例都涉及使用setTimeout在X秒后显示元素。 但我不确定这将如何应用于我的职能。
使用setTimeout:
setTimeout(function(){$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
.on('focus', function(){
var $this = $(this);
if($this.val() == '4/11/2013'){
$this.val('');
}
})
.on('blur', function(){
var $this = $(this);
if($this.val() == ''){
$this.val('4/11/2013');
}
});}, 3000);
您需要使用setTimeout(),因为另一方面它会触发一次setInterval() ,直到调用清除间隔为止。
$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input')
.on('focus', function(){
var $this = $(this);
if($this.val() == '4/11/2013'){
setTimeout(function(){
$this.val('4/11/2013');
}, 3000);
}
}).on('blur', function(){
var $this = $(this);
if($this.val() == ''){
setTimeout(function(){
$this.val('4/11/2013');
}, 3000);
}
});
setTimeout
setTimeout()方法将在指定的毫秒数后调用一个函数或对一个表达式求值。
setInterval
setInterval()方法以指定的时间间隔(以毫秒为单位)调用函数或计算表达式。
setInterval()方法将继续调用该函数,直到调用clearInterval()或关闭窗口为止。
jQuery具有自己的.delay()函数。 对于文档: http : //api.jquery.com/delay/
尽管我无法为自己的atm测试。 这可能会给您一个如何实现此目的的小主意。
<script type="text/javascript">
$('#FormsPageID table tr:nth-child(12) td:nth-child(2) div span span input').delay(3000).focus(function () {
var $this = $(this);
if($this.val() == '4/11/2013'){
$this.val('');
}
}).delay(3000).blur(function() {
var $this = $(this);
if($this.val() == ''){
$this.val('4/11/2013');
}
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.