需要清除currenttime >= deliverytime
的间隔。现在函数调用即使条件为真,倒计时也为负。如果remainingTime
到达' 00&#39,则需要停止计时器;
<script>
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
t= setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds;
if (--timer < 0) {
// timer = duration;
clearInterval(t);
}
}, 1000);
}
function test(){
var x = new Date();
var h = x.getHours();
var m = x.getMinutes();
var dd=new Date(new Date().getTime()).toLocaleString();
var d = Date.parse(dd) / 1000;
var ct=new Date(new Date().getTime()).toLocaleTimeString();
var currenttime = d;
var alarmtime=$("#alarmHidden").val();
var deliverytime=$("#deliveryHidden").val();
var remainingTime=parseFloat(deliverytime)-parseFloat(d);
if((currenttime >= alarmtime) && (currenttime <= deliverytime)){
var fiveMinutes =remainingTime;
display = document.querySelector(\'#time\');
startTimer(fiveMinutes, display);
}
if(currenttime >= deliverytime){
clearInterval(s);
clearInterval(t);
}
}
var s=setInterval(function(){ test(); }, 1000);
</script>
&#13;