Show widget and hide

Asked

Viewed 103 times

1

I have a system of Alerts where the div where the Alerts are with diplay None , so when a new Alert appears in my js I do a togle to appear that Alert on the screen like this

       $("#alerts").toggle( "slow" );

how do I make Alert stay only 2 seconds on the screen and then disappear

1 answer

4


Just use a .delay( milisegundos ) among the chained methods:

$("#alerts").show("slow").delay(5000).hide("slow");

See working on Codepen.

  • show("slow") shows the element;

  • delay(5000) wait 5 seconds (use 2000 for 2 seconds);

  • hide("slow") hides the element.


For those who don’t need jQuery for other things, here is a version with pure JS:

How to show an element for a few seconds and then hide it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.