简体   繁体   中英

Trying to fix a Javascript Countdown. I have no idea what I'm doing

HELP ME STACKOVERFLOW KENOBI, YOU'RE MY ONLY HOPE.

So last week I asked this question:

I need to make a timer that counts down to 4:30pm. So far, I can get it to either 4pm or 5pm, but not any minutes in between. I've managed to figure this much out:

<script type="text/javascript">

var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];

var suffix = "AM";

var hoursLeft = 16 - hours;
var minsLeft = 60 - minutes;

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

if (hours >= 18)
{
 document.write("Same day shipping has ended for today.");
} else

if ((n === "Saturday") || (n === "Sunday"))
{
document.write("Same day shipping is not available today.");
} else {

if (hoursLeft === 0) {
    document.write(minsLeft + " minutes left to have your order shipped today!");
} else {
    if (hoursLeft === 1) {
        document.write(hoursLeft + " hour and " + minsLeft + " minutes left to have your order shipped today!");
    } else {
    if ((hoursLeft === 0) && (minsLeft <= 30)){
    document.write(minsLeft + " minute left to have your order shipped today!");
    } else {

    if (minsLeft ===1) {
    document.write (hoursLeft + " hours and " + minsLeft + " minute left to have your order shipped today!");
    } else {


if(minsLeft===60){
minsLeft=0;
hoursLeft++;
n === "Monday";
n === "Tuesday";
n === "Wednesday";
n === "Thursday";
n === "Friday";
}

document.write(hoursLeft  + " hours and " + minsLeft + " minutes left to have your order shipped today!");
}
}
}
}
}

</script>

I need the time to count down to 4:30pm, but I've only figured out how to change the "var hoursLeft" to set it to "16 - hours" which gets me to 4pm, but can't figure out how to get it to 4:30pm. I thought I'd tweak the "var minsLeft" but got a weird bug that started to say stuff like "2 hours and -15 minutes left".

Anyone have any ideas?

Luckily, the brilliant UtopiaLtd responded with this:

As a commenter above suggested, do your calculations in minutes. Convert your hours to total minutes and make sure the minutes are within your parameters.

For displaying the minutes, just use the modulo operator to get the number of minutes on top of the current hour.

For example, if there are 185 minutes left and you want to display "3 hours and 5 minutes left",

var minutesLeft = 185;
var hours = Math.floor(minutesLeft/60);
var minStr = minutesLeft % 60;
var timeLeft = hours + " hours and " + minStr + " minutes left";

You'll want to use your own logic, of course, to keep from saying things like "1 hours."

But here's the kicker, I have no idea what I'm doing , our tech guy left and his replacement quit before he even started. As the nerdiest guy in the office, this has fallen on my lap, but I haven't worked in javascript before.

How do I plug in UtopiaLtd's solution into my original script? Hate to be a burden, but I'm way out of my element here. I feel like I'm polluting the site with my ineptness, but need help and you guys are the only thing I can think of.

Thanks in advance!

Okay, so... to preface this, this isn't a very elegant solution. It doesn't take into account timezones or any of the multitude of caveats that come with dealing with time. You should look into some kind of plugin that will help you with this.

This should help you in the mean time though. Converted all your time calculations to minutes then did math, then converted back to hours and minutes.

var hours = currentTime.getHours();
var minutes = currentTime.getMinutes() + (hours * 60); //Convert current time to minutes
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";

var n = weekday[d.getDay()];

var suffix = "AM";

var minsLeft = (16.5 * 60) - minutes //get total minutes remaining until 16.5 (4:30 pm)
var hoursLeft = Math.floor(minsLeft / 60); //Get how many hours are in our minsLeft
var minsLeft = minsLeft % 60; //remove the hours from the minsLeft

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM