admin管理员组文章数量:1418098
I came across a question asking how to fade out Bootstrap alerts after 5 seconds, and the answer was the following code. The issue is, it only works once, however as I have multiple alerts that are generated after an AJAX call this is no good. How do I make the following code run every time an alert is triggered?
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 5000);
I came across a question asking how to fade out Bootstrap alerts after 5 seconds, and the answer was the following code. The issue is, it only works once, however as I have multiple alerts that are generated after an AJAX call this is no good. How do I make the following code run every time an alert is triggered?
window.setTimeout(function() {
$(".alert").fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, 5000);
Share
Improve this question
edited Aug 19, 2013 at 18:03
madth3
7,34412 gold badges52 silver badges74 bronze badges
asked Aug 19, 2013 at 2:32
Benedict LewisBenedict Lewis
2,8237 gold badges41 silver badges81 bronze badges
1
- Show the code where "alerts are generated after an AJAX call", that's where you'll want to put the code to hide them. – No Results Found Commented Aug 19, 2013 at 2:38
2 Answers
Reset to default 6One possible solution is to use a custom event showalert
after the alert is shown and in the event handler remove the alert
$(document).on('showalert', '.alert', function(){
window.setTimeout($.proxy(function() {
$(this).fadeTo(500, 0).slideUp(500, function(){
$(this).remove();
});
}, this), 5000);
})
and
$('<div class="alert alert-error">' + 'some message' + '</div>').appendTo('#result').trigger('showalert');
Demo: Fiddle
I think it is because $(this).remove();
removes the alert from the DOM. You can actually just modify the content without regenerating the alert every time after the AJAX call.
本文标签: javascriptFade out multiple Bootstrap alertsStack Overflow
版权声明:本文标题:javascript - Fade out multiple Bootstrap alerts - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244406a2649488.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论