简体   繁体   English

尝试修复Java Countdown。 我不知道我在做什么

[英]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. 我需要制作一个倒数至下午4:30的计时器。 So far, I can get it to either 4pm or 5pm, but not any minutes in between. 到目前为止,我可以将其设置为下午4点或下午5点,但间隔不得为两分钟。 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. 我需要时间倒数到下午4:30,但我只想出了如何将“ var hoursLeft”更改为“ 16-hours”,使我到下午4点,但无法弄清楚如何到达下午4:30。 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". 我以为我会调整“ var minsLeft”,但是遇到了一个奇怪的错误,开始说“还剩2小时-15分钟”之类的东西。

Anyone have any ideas? 有人有想法么?

Luckily, the brilliant UtopiaLtd responded with this: 幸运的是,出色的UtopiaLtd对此做出了回应:

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", 例如,如果还剩185分钟,而您想显示“还剩3小时5分钟”,

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." 当然,您将希望使用自己的逻辑来避免说“ 1小时”之类的话。

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. 作为办公室里最讨厌的人,这已经落在我的腿上了,但是我以前没有使用过JavaScript。

How do I plug in UtopiaLtd's solution into my original script? 如何将UtopiaLtd的解决方案插入原始脚本中? 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript,不知道我在做什么 - JavaScript, no idea what I am doing 在Javascript中,是否有一个“如果...发现”的等效项,或者是一种紧凑的方式来执行我想做的事情? - In Javascript, is there an equivalent of a “find if”, or a compact way of doing what I'm trying to do? 我尝试在 javascript 中创建一个 while 循环。 我有我想在身体上做什么的例子 - I try to make a while loop in javascript. I have example of what i'm trying to do in body 尝试回叫功能时我做错了 - What I'm doing wrong when trying call back function 我不知道如何解决这个问题(php、javascript、mysql) - i have no idea how to fix this (php, javascript, mysql) 尝试编译我的反应项目时收到大量错误。 我完全不知道这意味着什么或如何开始修复它 - Recieving an influx of errors when trying to complie my react project. I quite literally have no idea what it means or how to begin to fix it 将Python代码实现为Javascript时,我在做什么错? - What i'm doing wrong when implementing Python code to Javascript? 我正在尝试将华氏度转换为摄氏度,但它不起作用我做错了什么? - Javascript(需要有一个表格和一个清除按钮) - I am trying to convert Fahrenheit to Celsius but it's not working what am i doing wrong? - Javascript (needs to have a form and a clear button) 我不知道这段代码是什么 - I have no idea what this code is 尝试使用Javascript调整点击文字的大小,我在做什么错? - Trying to resize text on click with Javascript, what am I doing wrong?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM