倒数计时器暂停和恢复按钮,如果语句不起作用,则为true或false

问题描述 投票:0回答:1

this.start是开始倒计时的功能。如果会话是true,倒计时只能起作用。 this.pause使会话false,并且应该停止this.start,但计时器永远不会停止;它只是不断倒计时。

该功能虽然在控制台中工作。 this.resume也什么也没做;计数器只是继续下降,虽然它应该在会话是true时工作。

在我使用setInterval进行倒计时之前,它确实奏效了,但在此之后我无法弄清楚如何恢复它。有什么理由说我的if声明不能在这里暂停和恢复倒数计时器吗?

let timer = function(sessionNumber2) {
  let session = true;
  console.log(Boolean(session));
  let currentInterval;

  //update counter every 1 second
  if (session == true) {
    this.start = function() {
      //hide start buttun and show other pause
      startButton.style.display = 'none';
      pause.style.display = 'unset';
      
      const then = new Date();
      const end = then.setMinutes(then.getMinutes() + sessionNumber2);

      currentInterval = setInterval(function() {
        //get Todays date and time
        let now = new Date().getTime();

        //find the distance between now and count down date
        distance = end - now;

        // Time calculations for minutes and seconds
        let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        let seconds = Math.floor((distance % (1000 * 60)) / 1000);

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

        time.innerHTML = minutes + ':' + seconds;
        console.log(distance);
        //if over do something
        if (distance <= 0) {
          clearInterval(start);
          timeName.innerHTML = 'break';
          time.innerHTML = 'done';
        }
      }, 1000)
    }
  }

  //paused
  this.paused = function() {
    pause.style.display = 'none';
    resume.style.display = 'unset';
    session = false;
    console.log(Boolean(session));
  }

  //resume
  this.resumed = function() {
    resume.style.display = 'none';
    pause.style.display = 'unset';
    session = true;
    console.log(Boolean(session));
  }
}
javascript html
1个回答
0
投票

正如GreatBigBore所说,你的问题的答案是调试。如果没有调试,从现在开始一两个小时就会出现另一个问题,并且发现它很难解决。 console.log()是一个非常基本的调试工具;你想快点毕业。 Chrome和Firefox都有很棒的调试器,可以让你在任何时候实际停止代码,查看变量的值等。任何时候遇到问题,都可以从调试器开始。了解不同变量的价值,找出代码实际上逐步进行的位置等。这是Chrome中调试的一个很好的入门:https://developers.google.com/web/tools/chrome-devtools/javascript/

祝好运!

编辑 - 一个额外的注释,一些可以帮助你极好地编写好代码的东西就是所谓的“linting”。 Linting会告诉你像hey you should use === instead of == here这样的东西。我说现在的行业标准是ESLint。如果你想学习写好的,最新的代码,设置ESLint :)

© www.soinside.com 2019 - 2024. All rights reserved.