如何使倒数计时器在特定的秒数内倒数?

时间:2018-02-12 11:04:52

标签: javascript jquery

我有以下代码:

var hours = Math.floor((timeLeft) / 3600);
var minutes = Math.floor((timeLeft - (hours * 3600)) / 60);
var seconds = Math.floor((timeLeft - (hours * 3600) - (minutes * 60)));

if (hours < "10") {
  hours = "0" + hours;
}
if (minutes < "10") {
  minutes = "0" + minutes;
}
if (seconds < "10") {
  seconds = "0" + seconds;
}

$("#" + divName).html(hours + ":" + minutes + ":" + seconds);

让我们假设这些值。

End date = 13 Feb 2018 
Now Date = 12 Feb 2018

23:59:59 Current Counter

和它的作品一样

23:59:58
23:59:57

现在我想在每秒钟显示这样的计数器我想根据我的要求减少2或3秒

23:59:59  
23:59:57
23:59:55

我该怎么做?

1 个答案:

答案 0 :(得分:-1)

setTimeout和setInterval都不准确,因此也不可靠。因此,我不想在每次迭代中累积错误,而是只使用Date.now()

来解决任何间隔的独立问题。

一个简单的版本是:

&#13;
&#13;
//a simple linear equation over time, independant of any interval
function clock(startValue=Date.now(), speed=1){
  let m=+speed||0, b=(+startValue||0)-m*Date.now();
  //every time you call this function, it will give you 
  //the value for the very moment you call this function
  //wether you do this every second, every hour of on every frame
  return function time(){
    let value = m*Date.now()+b, v = Math.abs(value);
    return {
      value,
      sign: value < 0? "-": "",
      ms: Math.floor(value)%1e3,
      seconds: Math.floor(v/1e3)%60,
      minutes: Math.floor(v/6e4)%60,
      hours: Math.floor(v/36e5)%24,
      days: Math.floor(v/864e5)
    }
  }
}

//I find these constants pretty handy when describing time
const MS=1,SECOND=1e3,SECONDS=SECOND,MINUTE=6e4,MINUTES=MINUTE,HOUR=36e5,HOURS=HOUR,DAY=864e5,DAYS=DAY,WEEK=6048e5,WEEKS=WEEK;

//this is more descriptive
let countDown = clock(24*HOURS, -10*SECONDS / SECOND);
//than this
//let countDown = clock(86400000, -10);

let output = document.querySelector('#time');
//a helper
const leadingZero = v => String(v).padStart(2,0);

//this is just rendering the current time
//it has nothing to do with computing the time
setInterval(function(){
  let { sign, hours, minutes, seconds } = countDown();
  let hms = [hours, minutes, seconds].map(leadingZero).join(":");
  
  output.textContent = sign + hms;
}, 100);
&#13;
<div id="time"></div>
&#13;
&#13;
&#13;

但通常我会使用更完整/多功能的实现

&#13;
&#13;
class Clock {
  constructor(value=Date.now(), speed=1){
    this.m = +speed || 0;
    this.b = +value || 0;
    this.p = true;
  }

  get value(){ return this.p? this.b: this.m*Date.now() + this.b }
  set value(arg){
    let value = +arg || 0;
    this.b = this.p? value: value - Date.now()*this.m;
  }


  get paused(){ return this.p }
  set paused(arg){
    let pause = !!arg;
    if(pause === this.p) return;
    this.b += this.m*Date.now() * (pause? 1: -1);
    this.p = pause;
  }


  get speed(){ return this.m }
  set speed(arg){
    let speed = +arg || 0;
    if(speed === this.m) return;
    if(!this.p)
      this.b += Date.now() * (this.m-speed);
    this.m = speed;
  }

  valueOf(){ return this.value; }

  start(){
    this.paused = false;
    return this;
  }

  stop(){
    this.paused = true;
    return this;
  }

  time(){
    let value = this.value, v = Math.abs(value);
    return {
      value,
      sign: value < 0? "-": "",
      ms: Math.floor(value)%1e3,
      seconds: Math.floor(v/1e3)%60,
      minutes: Math.floor(v/6e4)%60,
      hours: Math.floor(v/36e5)%24,
      days: Math.floor(v/864e5)
    }
  }
}


const MS=1,SECOND=1e3,SECONDS=SECOND,MINUTE=6e4,MINUTES=MINUTE,HOUR=36e5,HOURS=HOUR,DAY=864e5,DAYS=DAY,WEEK=6048e5,WEEKS=WEEK;

let speed = this.speed;
let countDown = new Clock(24*HOURS-1, speed.value);

let output = document.querySelector('#time');
this.startBtn.onclick = () => countDown.start();
this.stopBtn.onclick = () => countDown.stop();

speed.onchange = speed.onkeyup = () => {
  if(!isNaN(+speed.value)) countDown.speed = speed.value;
}

const leadingZero = v => String(v).padStart(2,0);

//this is just rendering the current time
//it has nothing to do with computing the time
setInterval(function(){
  let { sign, hours, minutes, seconds } = countDown.time();
  
  output.textContent = sign + [hours, minutes, seconds].map(leadingZero).join(":");
}, 100);

console.log(countDown);
&#13;
<div id="time"></div>
<input type="button" id="startBtn" value="start" />
<input type="button" id="stopBtn" value="stop" />
<input type="number" id="speed" value="-2" />
&#13;
&#13;
&#13;

这有帮助吗?