I got JS
as below. I want all bootstrap
alerts
to dissaper after 1 sec. It works just one time
but when i add more alerts they stays as they are. What is the cause of that?
JavaScript
x
19
19
1
$(document).ready(function () {
2
function flashh(message, category) {
3
if (category == "error") {
4
var icon = "icon-exclamation-sign";
5
category = "danger";
6
} else if (category == "success") var icon = "icon-ok-sign";
7
else var icon = "icon-info-sign";
8
$(
9
'<div class="alert alert-' +
10
category +
11
'"><i class="' +
12
icon +
13
'"></i> <a class="close" data-dismiss="alert">×</a>' +
14
message +
15
"</div>"
16
).prependTo("#putFlashMsg");
17
}
18
});
19
setTimeout:
JavaScript
1
10
10
1
$(document).ready(function () {
2
window.setTimeout(function () {
3
$(".alert")
4
.fadeTo(1000, 0)
5
.slideUp(1000, function () {
6
$(this).remove();
7
});
8
}, 5000);
9
});
10
Advertisement
Answer
That’s because setTimeout
is only called once. If you want to have multiple calls, use setInterval
, that is used exactly the same way but is called until clearInterval
stops it.