dja*_*ngo 6 javascript jquery timeout twitter-bootstrap
我正在使用示例中的动态引导警报.见下文.
如何添加超时功能,以便在X时间后警报自动关闭?
HTML:
<div id="alert_placeholder"></div>
Run Code Online (Sandbox Code Playgroud)
JQUERY:
bootstrap_alert = function() {
}
bootstrap_alert.warning = function(message) {
$('#alert_placeholder').append('<div class="alert alert-block fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4>Info!</h4>'+ message +'</div>');
}
bootstrap_alert.info = function(message) {
$('#alert_placeholder').append('<div class="alert alert-block alert-info fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4>Info!</h4>'+ message +'</div>');
}
Run Code Online (Sandbox Code Playgroud)
Geo*_*rge 10
创建一个删除第一个(因此,最旧的)警报的函数:
function alertTimeout(wait){
setTimeout(function(){
$('#alert_placeholder').children('.alert:first-child').remove();
}, wait);
}
Run Code Online (Sandbox Code Playgroud)
[0] 确保每次超时仅删除第一个警报.
然后在显示警报时调用该函数,参数是警报关闭的时间长度,以毫秒为单位:
bootstrap_alert.warning = function(message) {
$('#alert_placeholder').append('<div class="alert alert-block fade in"><button type="button" class="close" data-dismiss="alert">×</button><h4>Info!</h4>'+ message +'</div>');
alertTimeout(5000);
}
Run Code Online (Sandbox Code Playgroud)
请看这个jsFiddle