jQuery show for 5 seconds then hide

前端 未结 3 498
孤城傲影
孤城傲影 2020-12-07 07:37

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

How to display the message for 5 seconds then hide?

相关标签:
3条回答
  • 2020-12-07 08:11

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

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

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

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

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

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

    $("#myElem").show().delay(5000).queue(function(n) {
      $(this).hide(); n();
    });
    
    0 讨论(0)
  • 2020-12-07 08:17

    You can use the below effect to animate, you can change the values as per your requirements

    $("#myElem").fadeIn('slow').animate({opacity: 1.0}, 1500).effect("pulsate", { times: 2 }, 800).fadeOut('slow'); 
    
    0 讨论(0)
  • 2020-12-07 08:17

    Just as simple as this:

    $("#myElem").show("slow").delay(5000).hide("slow");
    
    0 讨论(0)
提交回复
热议问题