JavaScript计时器问题

时间:2019-04-17 12:58:44

标签: javascript

我刚刚用JavaScript做了一个时钟(100小时只是测试)。我不知道哪里出了问题,但这是行不通的。谁能帮忙吗?

我在代码中到处搜索,看起来没有什么问题。

        var hour,
            minute,
            second;
        hour = 100;
        minute = 0;
        second = 0;

        function changeTime() {
            second--;
            if (second < 0) {
                minute--;
            }
            if (minute < 0) {
                hour--;
            }
if (hour > 0 && minute > 0 && seconds > 0) {
            document.getElementById("clock").innerHTML = hour + ":" + minute + ":" + seconds;
} if (hour > 0 && minute > 0 && seconds = 0) {
        document.getElementById("clock").innerHTML = hour + ":" + minute + ": 00";
} if (hour > 0 && minute = 0 && seconds = 0) {
        document.getElementById("clock").innerHTML = hour + ":00: 00";
} if (hour > 0 && minute = 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = hour + ":00:" + seconds;
} if (hour = 0 && minute > 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = minute + ":" + seconds;
} if (hour = 0 && minute = 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = "0:" + seconds;
} if (hour = 0 && minute > 0 && seconds = 0) {
        document.getElementById("clock").innerHTML = minute + ":00";
} if (hour = 0 && minute = 0 && seconds = 0) {
        document.getElementById("clock").innerHTML = "00:00";
} 
            if (hour = 0 && minute = 0 && second = 0) {
                return;
            } else {
setInterval(changeTime, 50);
            }
        }

应在“ clock”段中写入“ 100:00:00”。相反,它什么也不写。

编辑:我将它每秒更改20次,这样我可以更轻松地看到一些可能性。

编辑2:电动Boogaloo:我已将其修复。多亏了sonyv(还有我的一两次调整)。

2 个答案:

答案 0 :(得分:3)

您正在为变量分配0,而不是对其进行检查,

应该使用“ ==”或“ ===”

而不是“ =”

“ ==”检查值

“ ===”检查值并输入推荐

尝试一下

      var hour,
              minute,
              second;
          hour = 100;
          minute = 0;
          second = 0;

          function changeTime() {
              second--;
              if (second < 0) {
                  minute--;
              }
              if (minute < 0) {
                  hour--;
              }
  if (hour > 0 && minute > 0 && second > 0) {
              document.getElementById("clock").innerHTML = hour + ":" + minute + ":" + second;
  } if (hour > 0 && minute > 0 && second == 0) {
          document.getElementById("clock").innerHTML = hour + ":" + minute + ": 00";
  } if (hour > 0 && minute == 0 && second == 0) {
          document.getElementById("clock").innerHTML = hour + ":00: 00";
  } if (hour > 0 && minute == 0 && second > 0) {
          document.getElementById("clock").innerHTML = hour + ":00:" + second;
  } if (hour == 0 && minute > 0 && second > 0) {
          document.getElementById("clock").innerHTML = minute + ":" + second;
  } if (hour == 0 && minute == 0 && second > 0) {
          document.getElementById("clock").innerHTML = "0:" + second;
  } if (hour == 0 && minute > 0 && second == 0) {
          document.getElementById("clock").innerHTML = minute + ":00";
  } if (hour == 0 && minute == 0 && second == 0) {
          document.getElementById("clock").innerHTML = "00:00";
  }
              if (hour == 0 && minute == 0 && second == 0) {
                  return;
              } else {
  setInterval(changeTime, 50);
              }
          }

答案 1 :(得分:0)

您提供的小时,分​​钟,秒输入将不会打印任何内容。我已经修改了您的代码,并每5秒钟调用一次changeTime()。另外,为什么要在开始时减少秒数?尝试以下代码:

    var hour, minute, seconds;
            hour = 100;
            minute = 1;
            seconds = 1;
    changeTime();
    function changeTime() {
    seconds--;
    if (seconds < 0) {
        minute--;
    }
    if (minute < 0) {
        hour--;
    }

    console.log(hour+" - "+ minute +" - "+ seconds);
    if (hour > 0 && minute > 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = hour + ":" + minute + ":" + seconds;
    }
    else if (hour > 0 && minute > 0 && seconds == 0) {
        document.getElementById("clock").innerHTML = hour + ":" + minute + ": 00";
    }else if (hour > 0 && minute == 0 && seconds == 0) {
        document.getElementById("clock").innerHTML = hour + ":00: 00";
    }else if (hour > 0 && minute == 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = hour + ":00:" + seconds;
    }else if (hour == 0 && minute > 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = minute + ":" + seconds;
    }else if (hour == 0 && minute == 0 && seconds > 0) {
        document.getElementById("clock").innerHTML = "0:" + seconds;
    }else if (hour == 0 && minute > 0 && seconds == 0) {
        document.getElementById("clock").innerHTML = minute + ":00";
    }else if (hour == 0 && minute == 0 && seconds == 0) {
        document.getElementById("clock").innerHTML = "00:00";
    }else{
        document.getElementById("clock").innerHTML = "test";
    } 
    if (hour == 0 && minute == 0 && seconds == 0) {
        return;
    } else {
        setInterval(changeTime, 5000);
    }
}