-1

显示的代码的目的是按时间添加一些提醒。这些案件是针对一周中的不同日子和那几天的特定时间的。奇怪的是,有些语句最不工作,但我看不出是什么破坏了代码:

    function refreshTime() {
  var now = getTime();
  $('#date').html(now.day + ', ' + now.date + '. ' + now.month);
  $('#time').html("<span class='hour'>" + now.hour + "</span>" + "<span class='minute'>" + now.minute + "</span>" + "<span class='second'>" + now.second + "</span>");
  if (now.day != "Sonntag" && now.day != "Samstag")
    {
    if (now.hour == "9" && now.minute >= "50")
      {
      var left = "60" - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      }
    if (now.hour == '11' && now.minute >= '50')
      {
      var left = '60' - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      } 
    if (now.hour == '14' && now.minute >= '50')
      {
      var left = '60' - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      }
    if (now.hour == "17" && now.minute >= "50")
      {
      var left = "60" - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      }
     else
     {
     $('#gh').html("");
     }      
    }
  if (now.day == "Samstag")
    {
    if (now.hour == "9" && now.minute >= "50")
      {
      var left = "60" - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      }
    if (now.hour == "12" && now.minute >= "50")
      {
      var left = "60" - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      } 
    if (now.hour == "13" && now.minute >= "50")
      {
      var left = "60" - now.minute;
      $('#gh').html("<span class='gh_remind'>Grosshandel einstellen in " + left + " Minuten!</span>");
      }     
    else
     {
     $('#gh').html("");
     }         
    }

}

谢谢你的帮助!

4

2 回答 2

2

字符串不是数字。除非您确实希望它是一个字符串,否则不要在数字周围使用引号。

> console.log("1">"50")
  false
> console.log("9">"50")
  true
于 2012-11-19T13:58:44.610 回答
2

考虑一下:

var a = 'a', b;
if (a == 'a') {
  b = 'right';
}
if (a == 'c') {
  b = 'wrong';
}
else {
  b = 'FOOBAR!';
}
alert(b);

即使您(可能)期望在该结构的末尾b存储价值,它 - 惊喜,惊喜!- 将包含。原因是如果你所有的s 都这样写,它们是相互独立的。第一个将字符串分配给变量 - 但第二个,通过分支,会很高兴地重新分配它(给一些 foobar)。'right'ifFOOBARif'right'belse

如果要创建ifs 链,请改用if - else if - else语法:

var a = 'a', b;
if (a == 'a') {
  b = 'right';
}
else if (a == 'c') {
  b = 'wrong';
}
else {
  b = 'FOOBAR!';
}
alert(b);

现在这会告诉你right事情,对吧?)

于 2012-11-19T14:05:20.797 回答