简体   繁体   English

原始Javascript中的双(多级)倒计时

[英]Double (Multi-level) Countdown in vanilla Javascript

Okay, so this is driving me nuts. 好吧,这真让我发疯。

I am trying to make a countdown that counts to a certain day of the week in a certain timezone... and once the timer hits zero then it changes the countdown timer's caption and counts down to another day of the week in a different timezone. 我正在尝试将某个时区的倒数计时到一周的某天...一旦计时器达到零,它将更改倒数计时器的标题并倒计时到另一个时区的一周的另一天。

Basically, I need it to show when Saturday starts in a certain timezone... and when it's Saturday I need it to count down to when Sunday starts in a different timezone... 基本上,我需要它显示周六在某个时区的开始时间...以及什么时候周六我需要倒数到周日在其他时区的时间开始...

The idea is the two time-zones are the earliest and latest time-zones on either side of the international date line... so "it's Saturday somewhere for n more days/hours/minutes/seconds" vs. "it will be Saturday somewhere in n days/hours/minutes/seconds" -- it's actually "Saturday somewhere" for 50 hours. 这个想法是,两个时区是国际日期变更线任一侧的最早和最新时区...因此,“它在星期六的某个地方还有n天/小时/分钟/秒”,而“现在是星期六n天/小时/分钟/秒的某处” –实际上是“星期六某处”持续50小时。

I believe it's between 3am Friday and 5am Sunday in my timezone, so that is the border of "it's Saturday somewhere" -- this doesn't account for changes from daylight savings time. 我认为这是我所在时区的星期五凌晨3点到星期日凌晨5点之间,所以这就是“星期六在某处”的边界-这不考虑夏令时的变化。

Another issue is that people viewing the countdown timer won't all be in my timezone... so I'm thinking I might need to set it up to do a little math with the time-zone offset. 另一个问题是,查看倒数计时器的人不会全部都在我的时区中。因此,我想我可能需要将其设置为使用时区偏移量进行一些数学运算。

I feel like I'm over-thinking this. 我觉得我想得太多了。 I've knit-picked my code to death and I just want to have a clean slate to hash this out from scratch. 我已经选择了我的代码到死,而我只想拥有一个干净的清单以从头开始将其哈希掉。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Per comments above, here's what I came up with... trying to keep it clear what's actually happening... (Edit: corrected...) 根据上面的评论,这就是我想出的...试图保持清楚实际发生的事情...(编辑:已更正...)

var currentTime = new Date().valueOf();
var wktime = currentTime/604800000;
wktime -= Math.floor(wktime);
wktime *= 604800000;
wktime -= 122400000;//milliseconds from zero time to the original saturday, so 0 is the start of saturday.
var lengthOfSaturday = (24+12+14)*60*60*1000; //just to make the math clear
if (wktime > lengthOfSaturday) {
  var msToNextSaturday = wktime-lengthOfSaturday;
} else {
  var timeLeftSaturday = lengthOfSaturday-wktime;
}

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

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