0

帮助我 StackOVERFLOW KENOBI,你是我唯一的希望。

所以上周我问了这个问题:

我需要做一个倒计时到下午 4:30 的计时器。到目前为止,我可以把它弄到下午 4 点或 5 点,但中间不能有任何分钟。我已经设法弄清楚了:

<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>

我需要时间倒计时到下午 4 点 30 分,但我只知道如何更改“var hoursLeft”以将其设置为“16 小时”,这让我到下午 4 点,但不知道如何到下午 4 点 30 分。我以为我会调整“var minsLeft”,但遇到了一个奇怪的错误,开始说“还剩 2 小时 -15 分钟”之类的东西。

有人有想法么?

幸运的是,出色的 UtopiaLtd 对此做出了回应:

正如上述评论者所建议的那样,在几分钟内完成您的计算。将您的小时数转换为总分钟数,并确保分钟数在您的参数范围内。

要显示分钟,只需使用模运算符即可获取当前小时之上的分钟数。

例如,如果还剩 185 分钟,您想显示“还剩 3 小时 5 分钟”,

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

当然,您需要使用自己的逻辑来避免说出“1 小时”之类的话。

但问题来了,我不知道我在做什么,我们的技术人员离开了,而他的替代者甚至在他开始之前就辞职了。作为办公室里最书呆子的人,这已经落在了我的腿上,但我以前没有使用过 javascript。

如何将 UtopiaLtd 的解决方案插入到我的原始脚本中?讨厌成为负担,但我在这里已经脱离了我的元素。我觉得我的无能正在污染网站,但需要帮助,你们是我唯一能想到的。

提前致谢!

4

1 回答 1

0

好的,所以......作为序言,这不是一个非常优雅的解决方案。它没有考虑时区或任何处理时间的警告。您应该研究某种可以帮助您解决此问题的插件。

不过,这应该同时对您有所帮助。将所有时间计算转换为分钟,然后进行数学运算,然后转换回小时和分钟。

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
于 2013-04-23T16:15:01.243 回答