admin管理员组文章数量:1178553
I have strange problem with the delay function here using the HTML function with it.
I set an HTML text by using $( '#element').html( 'Hello World');
After setting the text I want to get this text disappear in 3 seconds.
So next line I wrote:
$('#element').delay( 3000).html( ' ');
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.
But why doesn't this work. Its a pretty simple, wait 3 seconds then run the HTML function.
I have strange problem with the delay function here using the HTML function with it.
I set an HTML text by using $( '#element').html( 'Hello World');
After setting the text I want to get this text disappear in 3 seconds.
So next line I wrote:
$('#element').delay( 3000).html( ' ');
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.
But why doesn't this work. Its a pretty simple, wait 3 seconds then run the HTML function.
Share Improve this question edited Sep 6, 2019 at 10:23 double-beep 5,50419 gold badges40 silver badges48 bronze badges asked Sep 9, 2010 at 11:08 NovumCoderNovumCoder 4,6179 gold badges46 silver badges60 bronze badges2 Answers
Reset to default 25delay() defaults to the animation queue, for effects like fadeOut(), etc. You should use setTimeout() instead:
window.setTimeout(function () {
$("#element").html(' ');
}, 3000);
From http://api.jquery.com/delay/:
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()
isn't a queued function. If you want it to happen in order in the animation queue, you'll have to .queue()
it yourself, like this:
$('#element').delay(3000).queue(function(n) {
$(this).html(' '); n();
});
If you're not chaining animations or anything like this, use setTimeout()
or setInterval()
(whichever is appropriate to the situation) directly, .delay()
is just a wrapper for setTimeout()
and there's no reason to use extra code/complexity when there's no need.
本文标签: javascriptUsing delay with HTML or text setting doesn39t workStack Overflow
版权声明:本文标题:javascript - Using delay with HTML or text setting doesn't work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738044836a2054424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论