当我点击按钮时功能不起作用?

发布于 2025-01-09 06:59:37 字数 472 浏览 0 评论 0原文

当我单击按钮时,计时器应该显示倒计时器。但按钮不起作用。

let timerCounter = document.getElementById("timer-counter");
let timer;
let timerCount;

function startTimer() {
  timer = setInterval(function() {
    timerCount--;
    timerElement.textContent = "Time; " + timerCount;
    if (timerCount === 0) {
      clearInterval(timer);
    }
  });
}
startButton.addEventListener("click", startTimer);

when i click my button, a timer is supposed to display a countdown timer. But the button does not work.

let timerCounter = document.getElementById("timer-counter");
let timer;
let timerCount;

function startTimer() {
  timer = setInterval(function() {
    timerCount--;
    timerElement.textContent = "Time; " + timerCount;
    if (timerCount === 0) {
      clearInterval(timer);
    }
  });
}
startButton.addEventListener("click", startTimer);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的鱼塘能养鲲 2025-01-16 06:59:37

这是我到目前为止发现的:

  • 您正在减少 timerCount,需要指定它的初始值才能工作。
  • 您使用的是 timerElement 而不是您声明的 timerCounter
  • 您必须将第二个参数传递给 setInterval ,即 <代码>延迟。
const timerCounter = document.getElementById('timer-counter');
const startButton = document.getElementById('start-button');

let timer;
let timerCount = 30;

startButton.addEventListener('click', startTimer);

function startTimer() {
  timer = setInterval(function () {
    timerCount--;
    timerCounter.textContent = 'Time; ' + timerCount;
    if (timerCount === 0) {
      clearInterval(timer);
    }
  }, 1000);
}
<div id="timer-counter"></div>
<button id="start-button">Start</button>

This is what I found so far:

  • You are decrementing the timerCount, need to specify the initial value for it to work.
  • You're using timerElement instead of timerCounter that you've declared.
  • You must pass the second args to the setInterval which is delay.

const timerCounter = document.getElementById('timer-counter');
const startButton = document.getElementById('start-button');

let timer;
let timerCount = 30;

startButton.addEventListener('click', startTimer);

function startTimer() {
  timer = setInterval(function () {
    timerCount--;
    timerCounter.textContent = 'Time; ' + timerCount;
    if (timerCount === 0) {
      clearInterval(timer);
    }
  }, 1000);
}
<div id="timer-counter"></div>
<button id="start-button">Start</button>

墟烟 2025-01-16 06:59:37

这是一种稍微不同的方法,可以避免全局变量的一些问题。侦听器调用的函数初始化计数,然后返回单击按钮时调用的新函数(闭包)。它还使用了 setTimeout ,我发现它更容易理解。

// Cache your elements
const counter = document.querySelector('#counter');
const startButton = document.querySelector('button');

// Initialise your count variable
function startTimer(count = 30) {

  // Return a function that is called from
  // the listener
  return function loop () {

    // Disabled the button once it's been clicked
    if(!startButton.disabled) startButton.disabled = true;      

    counter.textContent = `Time: ${count}`;
    if (count > 0) {
      setTimeout(loop, 500, --count);
    }
  }

  loop();

}

// Call startTimer to initialise the count, and return
// a new function that is used as the listener
startButton.addEventListener('click', startTimer(), false);
<div id="counter"></div>
<button>Start</button>

Here's a slightly different approach that avoids some of the problems with global variables. The function the listener calls initialises the count, and then returns a new function (a closure) that is called when the button is clicked. It also uses setTimeout which I find more easy to understand.

// Cache your elements
const counter = document.querySelector('#counter');
const startButton = document.querySelector('button');

// Initialise your count variable
function startTimer(count = 30) {

  // Return a function that is called from
  // the listener
  return function loop () {

    // Disabled the button once it's been clicked
    if(!startButton.disabled) startButton.disabled = true;      

    counter.textContent = `Time: ${count}`;
    if (count > 0) {
      setTimeout(loop, 500, --count);
    }
  }

  loop();

}

// Call startTimer to initialise the count, and return
// a new function that is used as the listener
startButton.addEventListener('click', startTimer(), false);
<div id="counter"></div>
<button>Start</button>

宣告ˉ结束 2025-01-16 06:59:37

我相信这可以改进。

  • 在这个例子中,我们不会低于 0。
  • 我们不允许超时冲突(超时不会叠加,导致奇怪的计数速度)。
  • 当为 0 时,我们可以重置为原始数字。
const c = document.getElementById('timer-counter')
const b = document.getElementById('start-button')

let timer = false
let timerCount = 30

b.addEventListener('click', start)

function decrement() {

    if(timerCount < 0) {
        timerCount = 30
        timer = false
        return
    }

    c.innerText = `Count: ${timerCount}`
    timerCount--

    timer = setTimeout(decrement, 200)
}

function start() {
    if(timer) return

    decrement()
}
<div id="timer-counter"></div>
<button id="start-button">Start</button>

I'm sure this could be improved.

  • In this example we don't go below 0.
  • We don't allow timeout collisions ( timeouts don't stack causing weird counting speeds ).
  • We can reset to the original number when on 0.

const c = document.getElementById('timer-counter')
const b = document.getElementById('start-button')

let timer = false
let timerCount = 30

b.addEventListener('click', start)

function decrement() {

    if(timerCount < 0) {
        timerCount = 30
        timer = false
        return
    }

    c.innerText = `Count: ${timerCount}`
    timerCount--

    timer = setTimeout(decrement, 200)
}

function start() {
    if(timer) return

    decrement()
}
<div id="timer-counter"></div>
<button id="start-button">Start</button>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文