剩余时间到下一个5分钟 - Javascript

时间:2014-09-24 11:44:10

标签: javascript

我正在尝试显示接下来5分钟的剩余时间(扣到当前时间的整整5分钟,例如15:05,15:10 ..)

我能够在下一个小时(不是几分钟)的剩余时间内实现相同的目标:

<span class="timer"></span>
<script>
function secondPassed() {
    var cur_date = new Date();
    var hour = cur_date.getHours();
    var minutes = cur_date.getMinutes();
    var seconds = cur_date.getSeconds();
    var minutes_remain = parseInt(59 - parseInt(minutes));
    var seconds_remain = parseInt(60 - parseInt(seconds));

    var timers = document.getElementsByClassName('timer');
    for (var i = 0; i < timers.length; i++) {
        timers[i].innerHTML = minutes_remain+":"+seconds_remain;
    }

}

var countdownTimer = setInterval(secondPassed, 1000);
</script>

JSfiddle:http://jsfiddle.net/ov0oo5f7/

7 个答案:

答案 0 :(得分:3)

这样的东西?

&#13;
&#13;
function secondPassed() {
    var cur_date = new Date();
    var minutes = cur_date.getMinutes();
    var seconds = cur_date.getSeconds();
    
    var timers = document.getElementsByClassName('timer');
    for (var i = 0; i < timers.length; i++) {
        timers[i].innerHTML = (4 - minutes % 5) + ":" + (seconds >= 50 ? "0" : "") + (59 - seconds);
    }
    
}

var countdownTimer = setInterval(secondPassed, 1000);
&#13;
<span class="timer"></span>
&#13;
&#13;
&#13;

答案 1 :(得分:3)

您可以稍微减少代码并使其更准确,使用 setTimeout 并将函数调整到尽可能接近下一整秒。 setInterval 会慢慢漂移。

// Return the time to next 5 minutes as m:ss
function toNext5Min() {

  // Get the current time
  var secs = (new Date() / 1000 | 0) % 300;

  // Function to add leading zero to single digit numbers
  function z(n){return (n < 10? '0' : '') + n;}

  // Return m:ss
  return (5 - secs/60 | 0) + ':' + z(60 - (secs%60 || 60));
}

// Call the function just after the next full second
function runTimer() {
  console.log(toNext5Min());
  var now = new Date();
  setTimeout(runTimer, 1020 - now % 1000);
}

上述时间在整分钟内结束,因此在10:00:00显示为5:00。如果您更愿意显示0:00,那么 toNext5Min 的最后一行应为:

  return (5 - (secs? secs/60 : 5) | 0) + ':' + z(60 - (secs%60 || 60));

答案 2 :(得分:1)

这是您需要更改以使其按您希望的方式工作的内容:

var minutes = cur_date.getMinutes();
var seconds = cur_date.getSeconds();

尝试将其更改为:

var minutes_remain = 5 - minutes%5 - 1;
var seconds_remain = 59 - seconds;

在此处试试:jsFiddle

答案 3 :(得分:0)

将您的会议记录保留更改为以下行。它计算当前分钟模型5减去5,这是你需要的。

function secondPassed() {
    var cur_date = new Date();
    var hour = cur_date.getHours();
    var minutes = cur_date.getMinutes();
    var seconds = cur_date.getSeconds();
    var minutes_remain = parseInt(5 - parseInt(minutes%5));
    var seconds_remain = parseInt(60 - parseInt(seconds));

    var timers = document.getElementsByClassName('timer');
    for (var i = 0; i < timers.length; i++) {
        timers[i].innerHTML = minutes_remain+":"+seconds_remain;
    }

}

var countdownTimer = setInterval(secondPassed, 1000);

更新了小提琴http://jsfiddle.net/ov0oo5f7/2/

答案 4 :(得分:0)

如果你除以5,将它四舍五入,然后再乘以5,你可以计算下5分钟的间隔时间。

如果您在一分钟内完成,则需要处理,否则minutes_remain将显示为少于1分钟。

就准确性而言,您不需要parseInt无处不在,因为它们已经是所有数字。我试图让它尽可能高效。在任何情况下,您只需每秒检查一次,因此无法保证准确性。

http://jsfiddle.net/ov0oo5f7/6/

function secondPassed() {
    var cur_date = new Date();

    var hour = cur_date.getHours();

    // handle 0 seconds - would be 60 otherwise
    var seconds_remain = 60 - (cur_date.getSeconds() || 60);

    // handle ceil on 0 seconds - otherwise out by a minute
    var minutes = cur_date.getMinutes() + (seconds_remain == 0 ? 0 : 1);
    var minutes_remain = Math.ceil(minutes/5) * 5 - minutes;

    var timers = document.getElementsByClassName('timer');
    for (var i = 0; i < timers.length; i++) {
        timers[i].innerHTML = minutes_remain+":"+seconds_remain;
    }

}

var countdownTimer = setInterval(secondPassed, 1000);

答案 5 :(得分:0)

如果您正在寻找机器时间并将其计数5分钟,那么这可能会有所帮助 -

var startTime = new Date();
function secondPassed() {

    var cur_date = new Date();

    if(parseInt((cur_date - startTime)/1000) >300 ){
        window.clearInterval(countdownTimer);
    }
    var hour = cur_date.getHours();
    var minutes = cur_date.getMinutes();
    var seconds = cur_date.getSeconds();
    var minutes_remain = parseInt(59 - parseInt(minutes));
    var seconds_remain = parseInt(60 - parseInt(seconds));

    var timers = document.getElementsByClassName('timer');
    for (var i = 0; i < timers.length; i++) {
        timers[i].innerHTML = minutes_remain+":"+seconds_remain;
    }

}

var countdownTimer = setInterval(secondPassed, 1000);

答案 6 :(得分:0)

一个简单的方法是5分钟是5 * 60 * 1000毫秒,因此

var k = 5*60*1000;
var next5 = new Date(Math.ceil((new Date).getTime()/k)*k);