javascript - 如何制作一个从 02 :00 reaches 00:00 and then restarts at 06:00 开始的计时器

标签 javascript html css timer

我如何制作一个计时器,它从 02:00 开始倒计时,当它到达 00:00 时,它会在 06:00 重新开始,然后再次倒计时,直到到达 00:00 并从 00:00 开始向上计数。我已经有了到达 00:00 时的代码。

    var direction = 'down';
    var mins = 30;
    var secs = mins * 60;

    function colorchange(minutes, seconds) {
      var minutes = document.getElementById('minutes');
      var seconds = document.getElementById('seconds');
      var colon = document.getElementById('divide');

      var color;
      if (direction == 'up') {
        color = 'black';
      } else if (secs <= 30) {
        color = 'red';
      } else if (secs <= 59) {
        color = 'orange';
      }
      minutes.style.color = seconds.style.color = colon.style.color = color
    }

    function getminutes() {
      // minutes is seconds divided by 60, rounded down
      mins = Math.floor(secs / 60);
      return ("0" + mins).substr(-2);
    }

    function getseconds() {
      // take mins remaining (as seconds) away from total seconds remaining
      return ("0" + (secs - Math.round(mins * 60))).substr(-2);
    }

    function countdown() {
      setInterval(function() {
        var minutes = document.getElementById('minutes');
        var seconds = document.getElementById('seconds');

        minutes.value = getminutes();
        seconds.value = getseconds();
        colorchange(minutes, seconds);

        if (direction == 'down') {
          secs--;
          if (secs <= 0) {
            direction = 'up';
          }
        } else if (direction == 'up') {
          secs++;
        }
      }, 1000);
    }


    countdown();
<div id="timer" style="width: 90%;">
  <input id="minutes" type="text" style="width: 90%; border: none; background-   color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%;right: -2%;">
  <input id="seconds" type="text" style="width: 90%; border: none; background-color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%;right: -42%;">
  <span id="divide" style="width: 90%; border: none; background-color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%; padding-left: 42%;">:       </span>
</div>

最佳答案

var direction = 'down';
var first = true; // added
var mins = 2; // changed
var secs = mins * 60;

function colorchange() {
  var minutes = document.getElementById('minutes');
  var seconds = document.getElementById('seconds');
  var colon = document.getElementById('divide');

  var color="black";
  if (secs <= 30) {
    color = 'red';
  } else if (secs <= 59) {
    color = 'orange';
  }
  minutes.style.color = seconds.style.color = colon.style.color = color
}

function getminutes() {
  // minutes is seconds divided by 60, rounded down
  mins = Math.floor(secs / 60);
  return ("0" + mins).substr(-2);
}

function getseconds() {
  // take mins remaining (as seconds) away from total seconds remaining
  return ("0" + (secs - Math.round(mins * 60))).substr(-2);
}

function countdown() {
  setInterval(function() {
    var minutes = document.getElementById('minutes');
    var seconds = document.getElementById('seconds');
    if (direction == 'down') {
      secs--;
      if (secs <= 0) {
        if (first) { // added
          first=false;
          mins = 6;
          secs = mins*60;
        }
        else direction = 'up'; // added
      }
    } else if (direction == 'up') {
      secs++;
    }
    minutes.value = getminutes();
    seconds.value = getseconds();
    colorchange();
    
    
  }, 1000);
}


countdown();
<div id="timer" style="width: 90%;">
  <input id="minutes" type="text" style="width: 90%; border: none; background-   color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%;right: -2%;">
  <input id="seconds" type="text" style="width: 90%; border: none; background-color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%;right: -42%;">
  <span id="divide" style="width: 90%; border: none; background-color:none; font-size: 300px; font-weight: bold; position: fixed; bottom: 30%; padding-left: 42%;">:       </span>
</div>

关于javascript - 如何制作一个从 02 :00 reaches 00:00 and then restarts at 06:00 开始的计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31385953/

相关文章:

javascript - AB1234567 的正则表达式

javascript - Angular 2 中的模拟后端响应

javascript - 如何对输入类型范围进行分组并保持所有范围组合在一起的最大值为 100%

javascript - 为什么 Shiny 的 dateInput() 会导致键盘出现在手机中?

html - CSS 将 div 重叠在另一个具有相对位置/内联 block 的 div 上?

css - SVG:文本中的文本(或使用 css)

javascript - 为什么我的 Shadow dom 会破坏我的自定义元素?

javascript - 替换 Node 的 native 域模块

Javascript (Vue.js) 无法在移动设备和其他连接的设备上运行,但可以在运行本地主机的桌面上运行

javascript - 向上滚动后如何修复标题下方的div?