我创建了一个从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);
我没有添加任何内容作为开始方式,因为我不确定从哪里开始!任何帮助都会很棒。
答案 0 :(得分:1)
您可以使用一些基本的if语句来完成此操作(请参见下文),但是正如评论中的人们所说的,将其读为:59
而不是0:59
const timeSpan = document.querySelector('#test');
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 = 62 * 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);
if (minutes > 0) {
if(seconds < 10){
timeSpan.innerHTML = minutes + ':0' + seconds;
} else {
// Inserts the timer into the Span timer
timeSpan.innerHTML = minutes + ':' + seconds;
}
} else if(seconds < 10) {
timeSpan.innerHTML = ':0' + seconds;
} else {
timeSpan.innerHTML = ':' + 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);
<p id="test">
</p>
答案 1 :(得分:1)
您可以使用简单的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);