Show element and hide

1

I have an alerts system where the div where the alerts are is diplayed none, so when a new alert appears in my js I make a toggle to appear that alert on the screen so

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

How do I make the alert stay for only 2 seconds on the screen and then disappear

    
asked by anonymous 12.09.2016 / 01:16

1 answer

4

Just use .delay( milisegundos ) between the chained methods:

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

See working at Codepen .

  • show("slow") shows element;

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

  • hide("slow") hides the element.


For those who do not need jQuery for other things, here is a pure JS version:

  

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

    
12.09.2016 / 01:33