Javascript 卸下0';倒数计时器中的s

Javascript 卸下0';倒数计时器中的s,javascript,countdowntimer,Javascript,Countdowntimer,我创建了一个从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(); //

我创建了一个从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);
const分钟=10;
//获取页面加载的准确时间
const now=new Date().getTime();
//为实际倒计时所做的数学运算。所以10*60*1000+从页面加载中检索到的时间。
施工截止时间=分钟*60*1000+现在;
//这是一个函数,但它是一个JavaScript方法并调用函数。
设置间隔(()=>{
//获取当前时间
var currentTime=new Date().getTime();
//获取截止日期(10分钟)与当前时间之间的“距离”
var距离=截止日期-当前时间;
//发现这个方法能帮你算数,我必须查一下,然后在学校里进行研究
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
//将计时器插入范围计时器
timeSpan.innerHTML=分钟+':'+秒;
//间隔设置为1秒,因此计时器每秒刷新并倒计时
如果(秒<0){
确认('Alert For your User!');
window.location.reload();
}
}, 1000);

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

你可以用一些基本的if语句(见下文)来实现这一点,但正如评论中的人所说,让它读
:59
而不是
0:59

const timeSpan=document.querySelector(“#test”);
常数分钟=10;
//获取页面加载的准确时间
const now=new Date().getTime();
//为实际倒计时所做的数学运算。所以10*60*1000+从页面加载中检索到的时间。
施工截止日期=62*1000+现在;
//这是一个函数,但它是一个JavaScript方法并调用函数。
设置间隔(()=>{
//获取当前时间
var currentTime=new Date().getTime();
//获取截止日期(10分钟)与当前时间之间的“距离”
var距离=截止日期-当前时间;
//发现这个方法能帮你算数,我必须查一下,然后在学校里进行研究
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
如果(分钟>0){
如果(秒<10){
timeSpan.innerHTML=分钟+':0'+秒;
}否则{
//将计时器插入范围计时器
timeSpan.innerHTML=分钟+':'+秒;
}
}否则如果(秒<10){
timeSpan.innerHTML=':0'+秒;
}否则{
timeSpan.innerHTML=':'+秒;
}
//间隔设置为1秒,因此计时器每秒刷新并倒计时
如果(秒<0){
确认('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;

您还可以声明一个变量来保存对
间隔的引用

// 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);
if(秒<0){
确认('Alert For your User!');
//完成时清除间隔!
clearInterval(my_interval);
}
}, 1000);

代码段:
let timeSpan=document.querySelector(“timeSpan”);
常数分钟=1;
//获取页面加载的准确时间
const now=new Date().getTime();
//为实际倒计时所做的数学运算。所以10*60*1000+从页面加载中检索到的时间。
施工截止时间=分钟*60*1000+现在;
//这是一个函数,但它是一个JavaScript方法并调用函数。
//将间隔声明为变量,以便清除它!
让我的_interval=setInterval(()=>{
//获取当前时间
var currentTime=new Date().getTime();
//获取截止日期(10分钟)与当前时间之间的“距离”
var距离=截止日期-当前时间;
//发现这个方法能帮你算数,我必须查一下,然后在学校里进行研究
var分钟=数学楼层((距离%(1000*60*60))/(1000*60));
var秒=数学楼层((距离%(1000*60))/1000);
//检查秒数是否为一位数
如果(seconds.toString().length==1){seconds=“0”+seconds}
//检查分钟数是否为零(即falsy)
如果(!分钟)分钟=”;
//将计时器插入范围计时器
timeSpan.innerHTML=分钟+':'+秒;
//间隔设置为1秒,因此计时器每秒刷新并倒计时
如果(秒<0){
确认('Alert For your User!');
//完成时清除间隔!
clearInterval(my_interval);
}

}, 1000);与0:59相比,我认为:59读起来很差。也许只是不删除0然后继续?我有点困惑
.9
不是
.09
-不确定你的意思?@zfrisch-当它从10秒变为9秒时,如:10:9,他希望它读成:10:09。@TravisJ这是一个作业,我100%同意你,但是,这是讲师想要的。至于秒数,我希望它是:09,:08。。。发射型计算机断层扫描仪。。上面写着要在0之前…哇。。这么简单的方法!!哈哈!如果我必须预先准备,我会怎么做?我知道它处理的是
format()
method@ShaunStone你说的“预先准备”是什么意思此外,如果这解决了您的问题,请将其标记为已接受并upvote.done:)并且我的JavaScript体验很差。我就用你的答案。我感谢你的帮助@肖恩