Javascript 清除消息jQuery delay setTimeout
如何在几秒钟后使用delay jQuery函数而不是setTimeout清除字段消息?下面的代码将立即清除该值Javascript 清除消息jQuery delay setTimeout,javascript,jquery,Javascript,Jquery,如何在几秒钟后使用delay jQuery函数而不是setTimeout清除字段消息?下面的代码将立即清除该值 var $this = $(this), mid = $this.find("#emid"), contents = $this.find("#equestion"); if(contents.length<30){ contents.css('color','red') .val(
var $this = $(this),
mid = $this.find("#emid"),
contents = $this.find("#equestion");
if(contents.length<30){
contents.css('color','red')
.val('Error Message')
.delay(5000)
.val('');
}
var$this=$(this),
mid=$this.find(#emid”),
contents=$this.find(#equestion”);
如果(contents.length这有点棘手,但是您可以创建一个几乎不做任何事情的动画,通过回调清除该值
if(contents.length<30){
contents.css('color','red')
.val('Error Message').animate({
opacity: 1
}, 5000, function() {
contents.val('');
});
}
if(contents.length这有点棘手,但是您可以创建一个几乎不做任何事情的动画,通过回调清除该值
if(contents.length<30){
contents.css('color','red')
.val('Error Message').animate({
opacity: 1
}, 5000, function() {
contents.val('');
});
}
if(contents.lengthjQuery文档清楚地提到
.delay()方法最适合在排队的jQuery之间进行延迟
因为它是有限的,例如,它不能提供一种
取消延迟-.delay()不是JavaScript的本机
setTimeout函数,可能更适合某些用途
案例
因此,最好使用setTimeout函数
jQuery文档明确提到
.delay()方法最适合在排队的jQuery之间进行延迟
因为它是有限的,例如,它不能提供一种
取消延迟-.delay()不是JavaScript的本机
setTimeout函数,可能更适合某些用途
案例
因此,最好使用setTimeout函数
UPD:实际上,您可以简单地使用默认队列和自由链式效果(如show
)和非效果(如val
)。只需将后者包装在队列中
-dequeue
调用:
$("input")
.queue(function() { $(this).val("blah").dequeue() })
.delay(1000)
.queue(function() { $(this).val("").dequeue() })
.fadeOut(1000) // etc
/UPD
不,您不必(实际上也不应该)使用setTimeout。jQuery为此提供了一种很好的内置机制,称为。基本思想如下:在命名队列中“收集”函数或延迟:
$(elem).queue("queueName", function(next) { do something and call next() });
$(elem).queue("queueName", function(next) { do something else and call next() });
$(elem).delay(3000, "queueName");
$(elem).queue("queueName", function(next) { do something else and call next() });
然后调用dequeue()
开始处理:
$(elem).dequeue("queueName")
队列中的每个函数都会被一个接一个地调用,并按预期延迟工作
在操作中:UPD:实际上,您可以简单地使用默认队列和自由链式效果(如show
)和非效果(如val
)。只需将后者包装在队列中
-退出队列
调用:
$("input")
.queue(function() { $(this).val("blah").dequeue() })
.delay(1000)
.queue(function() { $(this).val("").dequeue() })
.fadeOut(1000) // etc
/UPD
不,您不必(实际上也不应该)使用setTimeout。jQuery为此提供了一种很好的内置机制,称为。基本思想如下:在命名队列中“收集”函数或延迟:
$(elem).queue("queueName", function(next) { do something and call next() });
$(elem).queue("queueName", function(next) { do something else and call next() });
$(elem).delay(3000, "queueName");
$(elem).queue("queueName", function(next) { do something else and call next() });
然后调用dequeue()
开始处理:
$(elem).dequeue("queueName")
队列中的每个函数都会被一个接一个地调用,并按预期延迟工作
实际上:setTimeout
是这里最整洁的选项。你不能。.delay
用于动画;setTimeout
基本上用于其他一切。我确信。delay
在内部也使用setTimeout
是这里最整洁的选项。你不能。delay
用于动画ions;setTimeout
基本上适用于其他所有功能。我确信.delay
也在内部使用setTimeout
。有趣。我非常喜欢这个,很有意义,谢谢。@code x73:事实上,解决方案更简单。我更新了帖子。有趣。我非常喜欢这个,很有意义,谢谢。@code x73:事实上,解决方案更简单离子更简单。我更新了帖子。