gpt4 book ai didi

javascript - 删除倒数计时器中的 0

转载 作者:行者123 更新时间:2023-11-30 13:59:20 26 4
gpt4 key购买 nike

我创建了一个从 10:00 开始倒计时的倒计时计时器,我希望倒计时计时器在低于 1 分钟时删除第一个 0。并在低于 10 秒时包含结尾零。

例如:“0:59”我想删除 0 所以它应该显示“:59”然后“:9”应该显示“:09”

老实说,我并没有尝试太多。我认为也许这可以用正则表达式来完成,但我不确定如何做。

我的计时器:

const mins = 10;
// getting the exact time as of the page load
const now = new Date().getTime();
// the math that is done for the actual countdown. So 10*60*1000 + the time retrieved from the page load.
const deadline = mins * 60 * 1000 + now;

// This is a function, however it is a JavaScript method and calls a function.
setInterval(() => {
// Gets the current time
var currentTime = new Date().getTime();
// gets the 'distance' between the deadline(10 mins) and the current time
var distance = deadline - currentTime;
// found out this method does the math for you, I had to look this up and research it on W3schools
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
// Inserts the timer into the Span timer
timeSpan.innerHTML = minutes + ':' + seconds;
// the interval is set to 1 sec, so the timer refreshes and counts down every second

if (seconds < 0) {
confirm('Alert For your User!');
window.location.reload();
}
}, 1000);

我没有在开始时添加任何东西,因为我不确定从哪里开始!任何帮助都会很棒。

最佳答案

回答:

您可以使用简单的 if 语句在输出到屏幕之前更改输出。

  // check if seconds is single digit
if(seconds.toString().length === 1) { seconds = "0" + seconds }
// check if minutes is zero ( which is falsy )
if(!minutes) minutes = "";
// Inserts the timer into the Span timer
timeSpan.innerHTML = minutes + ':' + seconds;

您还可以声明一个变量来保存对 interval

的引用
// declare the interval as a variable so you can clear it!
let my_interval = setInterval(() => {

这允许您在不再需要运行时清除它:

if (seconds < 0) {
confirm('Alert For your User!');
//clear the interval when it finishes!
clearInterval(my_interval);
}
}, 1000);

代码片段:

let timeSpan = document.querySelector("#timeSpan");
const mins = 1;
// getting the exact time as of the page load
const now = new Date().getTime();
// the math that is done for the actual countdown. So 10*60*1000 + the time retrieved from the page load.
const deadline = mins * 60 * 1000 + now;

// This is a function, however it is a JavaScript method and calls a function.

// declare the interval as a variable so you can clear it!
let my_interval = setInterval(() => {
// Gets the current time
var currentTime = new Date().getTime();
// gets the 'distance' between the deadline(10 mins) and the current time
var distance = deadline - currentTime;
// found out this method does the math for you, I had to look this up and research it on W3schools
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);

// check if seconds is single digit
if(seconds.toString().length === 1) { seconds = "0" + seconds }
// check if minutes is zero ( which is falsy )
if(!minutes) minutes = "";
// Inserts the timer into the Span timer
timeSpan.innerHTML = minutes + ':' + seconds;

// the interval is set to 1 sec, so the timer refreshes and counts down every second

if (seconds < 0) {
confirm('Alert For your User!');
//clear the interval when it finishes!
clearInterval(my_interval);
}
}, 1000);

关于javascript - 删除倒数计时器中的 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56637981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com