将延迟与HTML或文本设置一起使用不起作用 [英] Using delay with HTML or text setting doesn't work
问题描述
在这里,我将延迟函数与HTML函数一起使用时感到奇怪.
I have strange problem with the delay function here using the HTML function with it.
我使用$( '#element').html( 'Hello World');
设置完文字后,我要获取的文字会在3秒钟内消失.
After setting the text I want to get this text disappear in 3 seconds.
所以我写了下一行:
$('#element').delay( 3000).html( ' ');
这不起作用,它没有等待3秒钟就将HTML设置为 
,看起来jQuery跳过了delay函数.例如,将其与fadeOut配合使用时效果很好.我猜想这与延迟这个队列有关.
This one doesn't work, it sets the HTML to  
without waiting the 3 seconds, it looks like jQuery is skipping the delay function. Using this with fadeOut for example works fine. I guess this has something to do with this queue thing in delay.
但是为什么不起作用.这非常简单,请等待3秒钟,然后运行HTML函数.
But why doesn't this work. Its a pretty simple, wait 3 seconds then run the HTML function.
推荐答案
delay()默认为动画队列,用于诸如 fadeOut()等效果.应该使用 setTimeout()代替:
delay() defaults to the animation queue, for effects like fadeOut(), etc. You should use setTimeout() instead:
window.setTimeout(function () {
$("#element").html(' ');
}, 3000);
来自 http://api.jquery.com/delay/:
jQuery.delay()最适合在排队的jQuery效果与类似效果之间进行延迟,并且不能替代JavaScript的本机
jQuery.delay() is best for delaying between queued jQuery effects and such, and is not a replacement for JavaScript's native setTimeout function, which may be more appropriate for certain use cases.
这篇关于将延迟与HTML或文本设置一起使用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!