質問

I programmed this countdown where you define time without date and thats great but there is problem, if I want counter to end after midnight let's say at 1AM I can't, I can't because that time already passed. Does anybody have some idea how to fix that? It makes trouble only about midnight when it has to pass to other day(date), I use 24h format in code.

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
<script>
var timer;
function cdtd() {
    var now = new Date();
    var dolazak = new Date(now.getFullYear(),now.getMonth(),now.getDate(),23,00,00);
    var timeDiff = dolazak.getTime() - now.getTime();
    if (timeDiff <= 0) {
        clearInterval(timer);
                document.getElementById(id).innerHTML = '';
    }
    if(minutes < 2){document.getElementById(id).style.color="#ff0000";};
    var seconds = Math.floor(timeDiff / 1000);
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    var days = Math.floor(hours / 24);
    hours %= 24;
    minutes %= 60;
    seconds %= 60;
    $("#daysBox").html(days);
    $("#hoursBox").html(hours);
    $("#minsBox").html(minutes);
    $("#secsBox").html(seconds);

    timer = setTimeout(cdtd, 1000);
}

$(document).ready(function() {
     cdtd();
});
</script>

<style>
#daysBox{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
}

#hoursBox{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
}


#minsBox{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
}



#secsBox{font-size:70px;
         color:#1f62a7;
         font-family:Sans-serif;
}

</style>


    <div id="daysBox"></div>

    <div id="hoursBox"></div>

    <div id="minsBox"></div>

    <div id="secsBox"></div>
</body>
役に立ちましたか?

解決

@DontVoteMeDown is right.

If you wanted to do 1 am the following morning, just do this:

var dolazak = new Date(now.getFullYear(),now.getMonth(),now.getDate()+1,1,00,00);

The now.getDate()+1 makes dolazak be the next day, so ...1,00,00); sets the variable as 1:00am the next morning.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top