为什么我的倒计时不会停在0?

时间:2016-02-26 22:19:14

标签: javascript



//Not finished -- disregard
function evaluate() {
  var cdate = new Date();
  var cday = cdate.getDay();
  var chr = cdate.getHours();
  var cmin = cdate.getMinutes();
  var csec = cdate.getSeconds();
  //Luni
  if (cday === 1) {
    countDown(5, 0, 0, 100, "countdown")
  }
  //Marti sau Miercuri
  if ((cday === 2) || (cday === 3)) {
    countDown(5, 0, 0, 0, "countdown")
  }
  //Joi
  if (cday === 4) {
    countDown(0, 0, 0, 100, "countdown")
  }
  //Vineri sau Sambata
  if ((cday === 5) || (cday === 6)) {
    countDown(5, 0, 0, 0, "countdown")
  }
  //Duminica
  if (cday === 0) {
    countDown(5, 0, 0, 0, "countdown")
  }
}

function countDown(sec, min, hour, day, elem) {
  var element = document.getElementById(elem);
  element.innerHTML = "Urmatorul eveniment este in " + day + " zile " + hour + " ore " + min + " minute " + sec + " secunde.";
  if ((sec === 0) && (min > 0)) {
    sec = 59;
    min = min - 1;
  }
  if ((sec === 0) && (min === 0) && (hour > 0)) {
    sec = 59;
    min = 59;
    hour = hour - 1;
  }
  if ((sec === 0) && (min === 0) && (hour === 0) && (day > 0)) {
    sec = 59;
    min = 59;
    hour = 23;
    day = day - 1;
  }

  if ((min < 0) && (day > 0)) {
    min = 59;
    day = day - 1;
  }
  if ((min < 0) && (day === 0) && (day > 0)) {
    min = 59;
    hour = 23;
    day = day - 1;
  }

  if ((hour < 0) && (day > 0)) {
    hour = 23;
    day = day - 1;
  }
  //Here is the deadline if
  if ((day === 0) && (hour === 0) && (min === 0) && (sec < 0)) {
    clearTimeout(timer);
    elem.innerHTML = "Evenimentul este in decurs!";
  }
  sec--;
  var timer = setTimeout('countDown(' + sec + ',' + min + ',' + hour + ',' + day + ',"' + elem + '")', 1000);
}
evaluate();
&#13;
<p id="countdown"></p>
&#13;
&#13;
&#13;

我的JavaScript无效。当所有值都达到0时,计时器应该清除。

我的目标是:评估日期 - &gt;显示 - &gt;复位。

if ((day === 0) && (hour === 0) && (min === 0) && (sec < 0)) {
    clearTimeout(timer);
}

有人可以帮我这个吗? 我已经设置了一个代码段。提前谢谢。

2 个答案:

答案 0 :(得分:1)

您需要将条件更改为sec <= 0并减少else语句中的秒数

if ((day === 0) && (hour === 0) && (min === 0) && (sec <= 0)) {
    clearTimeout(timer);
    elem.innerHTML = "Evenimentul este in decurs!";
  }else{
    sec--;
    var timer = setTimeout('countDown(' + sec + ',' + min + ',' + hour + ',' + day + ',"' + elem + '")', 1000);
}

答案 1 :(得分:0)

清除定时器并输出消息后,您需要退出该功能(使用return)。

此外,邮件应输出到element而不是elem元素。

最后,应该在函数调用中保留 timer 值,因此将其设置为全局。只要定时器为0就可以工作,但是如果你有更多的定时器,分配的定时器值将不为零,除非你在函数之外保留了这个值,否则你将无法清除它。

这有效:

&#13;
&#13;
var timer;
 
//Not finished -- disregard
function evaluate() {
  var cdate = new Date();
  var cday = cdate.getDay();
  var chr = cdate.getHours();
  var cmin = cdate.getMinutes();
  var csec = cdate.getSeconds();
  //Luni
  if (cday === 1) {
    countDown(5, 0, 0, 100, "countdown")
  }
  //Marti sau Miercuri
  if ((cday === 2) || (cday === 3)) {
    countDown(5, 0, 0, 0, "countdown")
  }
  //Joi
  if (cday === 4) {
    countDown(0, 0, 0, 100, "countdown")
  }
  //Vineri sau Sambata
  if ((cday === 5) || (cday === 6)) {
    countDown(5, 0, 0, 0, "countdown")
  }
  //Duminica
  if (cday === 0) {
    countDown(5, 0, 0, 0, "countdown")
  }
}

function countDown(sec, min, hour, day, elem) {
  var element = document.getElementById(elem);
  element.innerHTML = "Urmatorul eveniment este in " + day + " zile " + hour + " ore " + min + " minute " + sec + " secunde.";
  //Here is the deadline if
  if ((sec === 0) && (min > 0)) {
    sec = 59;
    min = min - 1;
  }
  if ((sec === 0) && (min === 0) && (hour > 0)) {
    sec = 59;
    min = 59;
    hour = hour - 1;
  }
  if ((sec === 0) && (min === 0) && (hour === 0) && (day > 0)) {
    sec = 59;
    min = 59;
    hour = 23;
    day = day - 1;
  }

  if ((min < 0) && (day > 0)) {
    min = 59;
    day = day - 1;
  }
  if ((min < 0) && (day === 0) && (day > 0)) {
    min = 59;
    hour = 23;
    day = day - 1;
  }

  if ((hour < 0) && (day > 0)) {
    hour = 23;
    day = day - 1;
  }
  if ((day === 0) && (hour === 0) && (min === 0) && (sec < 0)) {
    clearTimeout(timer);
    element.innerHTML = "Evenimentul este in decurs!";
    return;
  }
  sec--;
  timer = setTimeout('countDown(' + sec + ',' + min + ',' + hour + ',' + day + ',"' + elem + '")', 1000);
}
evaluate();
&#13;
<p id="countdown"></p>
&#13;
&#13;
&#13;