【问题标题】:Setting remaining time variable as time from now to midnight?将剩余时间变量设置为从现在到午夜的时间?
【发布时间】:2017-11-07 06:43:26
【问题描述】:

您好,我是 JavaScript 新手,似乎无法理解如何更改此代码。

我想更改此设置,以便剩余时间是从用户加载页面到午夜。例如,如果是晚上 7 点,剩余时间将为 5 小时 0 分 0 秒,或者如果是晚上 7:45,剩余时间将为 4 小时 15 分 0 秒。

function startTimer(start, duration, display) {
    var diff, minutes, seconds;
    function timer(){
        diff = duration - (((Date.now() - start) / 1000) | 0);
        minutes = (diff / 60) | 0;
        seconds = (diff % 60) | 0;
        minutes = minutes < 10 ? "0" + minutes : minutes;
        seconds = seconds < 10 ? "0" + seconds : seconds;
        display.textContent = minutes + ":" + seconds; 
        if (diff <= 0) {
            clearInterval(inti);
            document.getElementById("countdownhere").innerHTML

编辑:哎呀,这是完整的代码,感谢 Jonas W 指出这一点!

var d = new Date();

(function($){$.fn.progressbar=function(){var a="<p>Countdown Timer!</p>"+"<div class='progressbar'><div style='width:100%'></div></div>";this.addClass('items-count');this.html(a+this.html());updateMeter(this);var b=this;

{$("#progress_bar").progressbar();var tag="ctdn-12-12".match(/\d+/g);var hour=14;var theDaysBox=$("#numdays");var theHoursBox=$("#numhours");var theMinsBox=$("#nummins");var theSecsBox=$("#numsecs");var d=new Date();var n=d.getDay();var date=1;var gg=0;var hh=0;var ii=0;var nsec=0-d.getSeconds();if(nsec<0){nsec=60-d.getSeconds();gg=1}var nmin=0-d.getMinutes()-gg;if(nmin<0){nmin=60-d.getMinutes()-gg;hh=1}var nhrs=14-d.getHours()-hh;if(nhrs<0){nhrs=38-d.getHours()-hh;ii=1}var ndat=date-1;if(ndat<0){var mmon=d.getMonth();ndat=30+date-d.getDate()-ii}theSecsBox.html(nsec);theMinsBox.html(nmin);theHoursBox.html(nhrs);theDaysBox.html(ndat);var refreshId=setInterval(function(){var e=theSecsBox.text();var a=theMinsBox.text();var c=theHoursBox.text();var b=theDaysBox.text();if(e==0&&a==0&&c==0&&b==0){}else{if(e==0&&a==0&&c==0){theDaysBox.html(b-1);theHoursBox.html("23");theMinsBox.html("59");theSecsBox.html("59")}else{if(e==0&&a==0){theHoursBox.html(c-1);theMinsBox.html("59");theSecsBox.html("59")}else{if(e==0){theMinsBox.html(a-1);theSecsBox.html("59")}else{theSecsBox.html(e-1)}}}}},1000);});</script>

<div class="items-count" id="progress_bar"></div><div id="clock-ticker" class="clearfix"><div class="block"><span class="flip-top" id="numdays">0</span><br><span class="label">Days</span></div><div class="block"><span class="flip-top" id="numhours">1</span><br><span class="label">Hours</span></div><div class="block"><span class="flip-top" id="nummins">23</span><br><span class="label">Minutes</span></div><div class="block"><span class="flip-top" id="numsecs">36</span><br><span class="label">Seconds</span></div>

【问题讨论】:

  • 试试moment.js,你可以使用moment().endOf('day')
  • 嗯那里缺少代码...
  • 乔纳斯 w - 谢谢你是对的!我已经编辑并修复了,对不起!你能更新你的答案吗?谢谢

标签: javascript time timer


【解决方案1】:

也许:

var midnight=new Date();
midnight.setHours(0);
midnight.setDate(midnight.getDate()+1);
var display=document.body;
startTimer(new Date(),midnight,display);

SetHours 将日期设置为今天的午夜,然后我们得到第二天......

【讨论】:

    猜你喜欢
    • 2014-08-26
    • 1970-01-01
    • 2012-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多