[英]I'm having some problem with setInterval()
I want my stopwatch as if 60 seconds complete .我想要我的秒表好像 60 秒完成。 There should be an increment in minute and seconds to start from 0 again .
分秒之间应该有一个增量,以便再次从 0 开始。 I tried so many ways to do so but it always stop working when time is one minute .. is it built in problem in
setInterval()
我尝试了很多方法来做到这一点,但是当时间为一分钟时它总是停止工作..它是否存在于
setInterval()
中的问题
async timer() {
var timeout = setInterval(() => {
count++
this.timerSecond.innerText = count;
if (count > 59) {
count = 0;
this.timerSecond.innerText = count;
count1++
this.timerMinute.innerText = count1
}
}, 100);
console.log(timeout);
return timeout;
}
Does this method work for you?这个方法对你有用吗?
timer () {
let seconds = 0;
const tick = () => {
this.timerText.textContent = seconds;
this.timerSecond.textContent = `${seconds % 60}`.padStart(2, '0');
this.timerMinute.textContent = Math.floor(seconds / 60);
seconds++;
};
tick();
return setInterval(tick, 1000);
}
It's hard to tell why you had two separate setInterval()
calls, but I removed the one called every 100 milliseconds and combined the logic into a single one.很难说为什么有两个单独的
setInterval()
调用,但我删除了每 100 毫秒调用一次的调用,并将逻辑合并为一个。
The timerSecond
uses modulo 60 of seconds
, and timerMinute
uses result of integer division by 60, while the timerText
just receives the seconds
directly, as in your initial code. timerSecond
使用seconds
模 60 ,而timerMinute
使用整数除以 60 的结果,而timerText
只是直接接收seconds
,就像在您的初始代码中一样。
The async
keyword didn't add any value to your code, since none of it uses promises, so I removed it. async
关键字没有为您的代码添加任何值,因为它没有使用承诺,所以我删除了它。
Here's a slightly elaborated example to demonstrate functionality:这是一个稍微详细说明的示例来演示功能:
class Stopwatch { timerText = document.querySelector('.text'); timerSecond = document.querySelector('.second'); timerMinute = document.querySelector('.minute'); timer () { let seconds = 0; const tick = () => { this.timerText.textContent = seconds; this.timerSecond.textContent = `${seconds % 60}`.padStart(2, '0'); this.timerMinute.textContent = Math.floor(seconds / 60); seconds++; }; tick(); return setInterval(tick, 1000); } } new Stopwatch().timer();
<div class="text"></div> <div> <span class="minute"></span>:<span class="second"></span> </div>
This is nice and simple:这很好很简单:
var seconds = 0; setInterval(function(){ tick(document.getElementById("timer"), ++seconds); }, 1000); function tick(ele, secs){ ele.innerHTML = Math.floor(secs / 60) + ":" + (secs % 60 < 10 ? "0" : "") + secs % 60; }
<span id="timer">0:00</span>
Math.floor(secs / 60)
gives us the minutes and excludes any remainder of seconds, secs % 60
gives us the remainder of seconds after we've divided by 60 (so it essentially removes the minutes), and (secs % 60 < 10 ? "0" : "")
gives us a leading "0" if the seconds (excluding whole minutes) is less than 10. Math.floor(secs / 60)
为我们提供分钟数并排除任何剩余的秒数, secs % 60
为我们提供除以 60 后剩余的秒数(因此它基本上删除了分钟数),并且(secs % 60 < 10 ? "0" : "")
如果秒数(不包括整分钟)小于 10,则给我们一个前导“0”。
Here is a basic example of how to make a counter that counts down from 60 (or any other number) and display it on the page.这是一个基本示例,说明如何制作一个从 60(或任何其他数字)开始倒计时并将其显示在页面上的计数器。
// Reference to html element that holds the count const counterDiv = document.getElementById('counter'); // Variable that will keep track of the seconds to count down from const secondsToCount = 60; // Set the initial value of the counter to the secondsToCount variable. // The counter will be updated each 1000ms | 1s and will be changed // to the value of the remaining seconds to count down from // which is why this variable is let opposed to const let counter = secondsToCount; // Set the initial text of the counter html element counterDiv.innerHTML = secondsToCount; // Function that is going to do counting for us. const interval = setInterval( () => { // Decrement the current counter value by 1 counter--; // Update the text to show the new value of our counter counterDiv.innerHTML = counter; // If the counter is === 0, the counter is finished. She some text. if (counter === 0) { // Clear the interval (otherwise, you'll continue to count // into the negative numbers clearInterval(interval); // Show the completed text counterDiv.innerHTML = `Done counting down from ${secondsToCount} seconds`; } }, 1000); // 1000 ms | 1s
<!-- Element used to show the count --> <div id="counter"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.