带有集成倒数计时器的Javascript时钟

时间:2015-10-14 13:29:37

标签: javascript timer clock

我需要使用倒数计时器对JavaScript时钟进行编码,该计时器在达到特定时间后开始倒计时5分钟。所以我有我的时钟和它的工作,但我不知道从哪里开始,我真的是初学者,当涉及到JavaScript而我还在学习绳索(有点慢)Can Can你请给我一些关于如何将我的时钟与我的倒数计时器(我不知道如何编码)整合的指导,以便在达到一定时间后,倒数计时器将开始倒计时5分钟。

继承我的代码:

$(document).ready(function() {

  function displayTime() {
    var currentTime = new Date ();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();

    if (minutes < 10) {
      minutes = "0" + minutes;
    }

    if (seconds < 10) {
      seconds = "0" + seconds;
    }

    var clockDiv = document.getElementById('clock')

    clockDiv.innerText = hours + ":" + minutes + ":" + seconds;


  }

  displayTime();
  setInterval(displayTime, 1000);

  function countDown() {
    var currentTime = new Date();
    var hours = currentTime.getHours();
    var minutes = currentTime.getMinutes();
    var seconds = currentTime.getSeconds();

    if (minutes < 10) {
      minutes = "0" + minutes;
    }

    if (seconds < 10) {
      seconds = "0" + seconds;
    }

  if 


  }

});

1 个答案:

答案 0 :(得分:1)

因此集成部分可以作为一些简单的If语句来完成。 假设您希望倒计时从上午9:00开始

1)创建一个布尔变量,看看我们是否应该显示当前时间或倒计时。这将非常方便,让我们无需检查RANGE。请注意,我们不能简单地检查当前时间,因为这只会显示倒计时一秒钟。

2)添加一个简单的if语句。如果是时间,通过将变量设置为true(9:00:00)来切换到倒计时

3)添加第二个if语句以切换回显示倒计时结束时的当前时间。这是可选的,如果您想要倒数,即使它已经结束,您也可以将其删除。注意:为了将来使用,将此功能委派为更明智,而不是静态倒计时为5分钟。你应该有一个变量说var totalcountdown = ...并说if(... minutes = 0 + totalcountdown。但是你需要在这里包含逻辑。

4)添加最终陈述以显示倒计时或当前时间

5)添加倒计时代码。有关此部分,请参阅下面的帖子。我无法解释它比这更好。

The simplest possible JavaScript countdown timer?

**重要说明:**当您从其他帖子复制并粘贴代码时,这将无法立即生效。你需要在最终的if语句中调整一些东西。我会把它作为锻炼给你。如果您需要帮助,只需发表评论或打开新问题

$(document).ready(function() {

   function displayTime() {
      var displayCountdownp = false;
      var currentTime = new Date ();
      var hours = currentTime.getHours();
      var minutes = currentTime.getMinutes();
      var seconds = currentTime.getSeconds();

      if (minutes < 10) {
         minutes = "0" + minutes;
      }

      if (seconds < 10) {
        seconds = "0" + seconds;
      }

      var clockDiv = document.getElementById('clock')

      if( hours==9 && minutes==0 && seconds==0){
          displayCountdownp = true;
      }

      if( hours==9 && minutes== 6 && seconds==0){
          displayCountdownp = false;
      }

      if(displayCountdownp){

        var displayValue = countDown();
        clockDiv.innerText = displayValue;

      }else{
        clockDiv.innerText = hours + ":" + minutes + ":" + seconds;
      }


     displayTime();
     setInterval(displayTime, 1000);

    function countDown() {
         //code goes here
    }
});
相关问题