jQuery显示5秒钟然后隐藏 [英] jQuery show for 5 seconds then hide

查看:278
本文介绍了jQuery显示5秒钟然后隐藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

成功提交表单后,我正在使用.show显示隐藏的消息.

I'm using .show to display a hidden message after a successful form submit.

如何显示消息5秒钟然后隐藏?

How to display the message for 5 seconds then hide?

推荐答案

您可以使用 .delay() 在动画之前,像这样:

You can use .delay() before an animation, like this:

$("#myElem").show().delay(5000).fadeOut();

如果不是动画,请直接使用 setTimeout() ,如下所示:

If it's not an animation, use setTimeout() directly, like this:

$("#myElem").show();
setTimeout(function() { $("#myElem").hide(); }, 5000);

之所以要第二次,是因为.hide()通常没有持续时间就不会出现在动画(fx)队列中,这只是即时效果.

You do the second because .hide() wouldn't normally be on the animation (fx) queue without a duration, it's just an instant effect.

或者,另一种选择是使用 .delay()

Or, another option is to use .delay() and .queue() yourself, like this:

$("#myElem").show().delay(5000).queue(function(n) {
  $(this).hide(); n();
});

这篇关于jQuery显示5秒钟然后隐藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆