Remove alert bar in php/javascript after specific interval

1.7k Views Asked by At

I have an alert box being shown in my webpage with the code :

Now I wish this alert box to stay for round about 2 seconds.

I have tried using :

<script>
 $(".alert-message").alert();
 window.setTimeout(function() { $(".alert-message").alert('close'); }, 2000);
</script>

This script tag is being used at the end of all the div tags used my webpage(.phtml file)

Problem is that I am able to close the alert bar by clicking the a href "x" button.

But I am not able to remove/disable/close this alert bar automatically after 2 seconds.

Anything wrong with my code ?

3

There are 3 best solutions below

1
Satish Sharma On BEST ANSWER

you can try this

setTimeout(function(){
    $('.feedback').fadeOut(1000);
    // $('.feedback').hide(1000); // you can also try this
}, 2000);

DEMO

0
Olim Saidov On

You forgot to add data-dismiss="alert" to close button.

Final look:

<a class="close" data-dismiss="alert" href="#">&times;</a>
0
codewizard On

JavaScript, to my knowledge doesn't provide any closing ability on the alert object outside of user input.

The best option would be to provide a user with a modal that you create and then destroy with setTimeout. Or if you choose to animate it out you could use jquery's delay function.