帮助重复的javascript倒计时和时区

时间:2011-01-27 17:22:37

标签: javascript refactoring timezone countdown

目标: 创建我们下一个可用直播的倒计时。

详细信息: 我们每周六次(PST)直播  1.星期日上午8点  2.星期日上午10点  3.星期日中午12点  星期天下午6:30  星期三晚上7点左右  6.星期六上午10点。

我的方法: 最初我检查它是哪一天以及它是什么时间然后创建下一个流的倒计时。这适用于我们的时区(PST)和任何相对接近的时区。

问题: 第一天检查打破了我对时区人们的态度,让他们领先我们一天。例如,如果来自伦敦的某人在星期四凌晨2:30(格林尼治标准时间)检查该网站,我的代码将开始倒计时到周六上午10:00流而不是周三晚上7:00。 (PST)将在30分钟后开始。

我确信我所做的事情可以清理和改进,告诉我如何做。

我的代码:

// Countdown
function countDown( addDay, hour, min ) {
    var newCount = new Date(); 
    newCount = new Date(newCount.getFullYear(), newCount.getMonth(), newCount.getDate() + addDay, hour, min); 
    $('#service_countdown').countdown({until: $.countdown.UTCDate(-8, newCount)}); 
}

var day_ref, day, addDay, hour, min;
addDay = 0;
hour = 0;
min = 0;
day = {};

day_ref = new Date();
day = {"day" : day_ref.getDay(), "hour" : day_ref.getHours(), "min" : day_ref.getMinutes()};

// It's Sunday
if ( day.day === 0 ) {

    if ( day.hour < 8 ) {
          hour = 8;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } 
    else if ( day.hour < 12 ) {
          hour = 12;
        countDown(addDay, hour, min);
    }
    else if ( day.hour <= 18 && day.min < 30 ) {
          hour = 18;
           min = 30;
        countDown(addDay, hour, min);
    } 
    else {
        addDay = 3;
          hour = 19;
        countDown(addDay, hour, min);
    }
} 
// It's Monday
else if ( day.day === 1 ) {
    addDay = 2;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Tuesday
else if ( day.day === 2 ) {
    addDay = 1;
      hour = 19;
    countDown(addDay, hour, min);
} 
// It's Wednesday
else if ( day.day === 3) {

    if ( day.hour < 19 ) {
          hour = 19;
        countDown(addDay, hour, min);
    } else {
        addDay = 3;
          hour = 10;
        countDown(addDay, hour, min);
    }
} 
// It's Thursday
else if ( day.day === 4 ) {
    addDay = 2;
      hour = 10;
    countDown(addDay, hour, min);
} 
// It's Friday
else if ( day.day === 5 ) {
    addDay = 1;
      hour = 10;
    countDown(addDay, hour, min);
} 
// All that's left is Saturday
else {

    if ( day.hour < 10 ) {
          hour = 10;
        countDown(addDay, hour, min);
    } else {
        addDay = 1;
          hour = 8;
        countDown(addDay, hour, min);
    }
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我最终只是一直在计算server side。在bob-the-destroyer的帮助下,根据要求,工作php非常紧凑。

<?php

$schedule = array(
    'this Sunday 8am',
    'this Sunday 10am',
    'this Sunday 12pm',
    'this Sunday 6:30pm',
    'this Wednesday 7pm',
    'this Saturday 10am'
    );

$current_time = strtotime('now');
foreach ($schedule as &$val) {
    $val = strtotime($val);
    // fix schedule to next week if time resolved to the past
    if ($val - $current_time < 0) $val += 604800; 
    }
sort($schedule);
$countdown = $schedule[0] - $current_time;

?>

<script type="text/javascript">
    var myTime = <?php echo $countdown; // just personally prefer full tags ?>;
    $('#countdown').countdown({ until: myTime}); 
</script>