这是我的JSP代码
这里我使用alertify.js进行通知,并使用javascript获取时间。 在一定时间内,它会显示8次通知。
(function () {
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
function startTime() {
var today = new Date(),
h = checkTime(today.getHours()),
m = checkTime(today.getMinutes()),
s = checkTime(today.getSeconds());
// ms = checkTime(today.getMilliseconds());
document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function () {
startTime()
}, 100);
if(h==16 && m==52 && s==00 )
{
alertify.notify('sample', 'success', 60, function(){ console.log('dismissed'); });
}
else if(h==17 && m==01 && s==00 )
{
alertify.notify('difficult', 'success', 60, function(){ console.log('dismissed'); });
}
}
startTime();
})();
&#13;
<script src="js/alertify.min.js"></script>
<!-- include the style -->
<link rel="stylesheet" href="css/alertify.min.css" />
<!-- include a theme -->
<link rel="stylesheet" href="css/default.min.css" />
<!-- CSS -->
<!-- CSS -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/alertify.min.css"/>
<!-- Default theme -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/themes/default.min.css"/>
<!-- Semantic UI theme -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/themes/semantic.min.css"/>
<!-- Bootstrap theme -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/themes/bootstrap.min.css"/>
<!--
RTL version
-->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/alertify.rtl.min.css"/>
<!-- Default theme -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/themes/default.rtl.min.css"/>
<!-- Semantic UI theme -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/themes/semantic.rtl.min.css"/>
<!-- Bootstrap theme -->
<link rel="stylesheet" href="//cdn.jsdelivr.net/alertifyjs/1.4.1/css/themes/bootstrap.rtl.min.css"/>
&#13;
<div id="time"></div>
&#13;
我希望在一定时间内看到一个通知。但怎么可能呢?
答案 0 :(得分:0)
您每秒检查时间10次(每100毫秒),这可能就是您收到许多警报的原因。将100替换为1000,它应该可以工作:
function startTime() {
var today = new Date(),
h = checkTime(today.getHours()),
m = checkTime(today.getMinutes()),
s = checkTime(today.getSeconds());
// ms = checkTime(today.getMilliseconds());
document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function () {
startTime()
}, 1000);
// ^^^^
if(h==16 && m==52 && s==00 )
{
alertify.notify('sample', 'success', 60, function(){ console.log('dismissed'); });
}
else if(h==17 && m==01 && s==00 )
{
alertify.notify('difficult', 'success', 60, function(){ console.log('dismissed'); });
}
}