2011-01-27 27 views
0

目標: 要創建一個倒計時,以我們的下一個可用的實時流。幫助與經常性的JavaScript的倒計時,時區

詳情: 我們每週直播6次(PST)。
1.週日上午8點00分
2.週日上午10:00點
3.週日下午12點
4.週日下午6點30分
5.星期三下午7點
6.週六上午10:00

我的方法: 本來我檢查一下是什麼日子,什麼時候它然後創建倒數至下一次流。這適用於我們的時區(PST)和任何相對接近的工作。

問題: 首先檢查一天休息,我的人的做法在把他們提前一天我們的時區。例如,如果倫敦某人在星期四上午2點30分(格林威治標準時間)對網站進行檢查,我的代碼將在星期六上午10點開始倒計時,而不是在星期三下午7點倒計時。 (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); 
    } 
} 

在此先感謝您的幫助!

+0

這可能更適合http://codereview.stackexchange.com - 雖然它絕對是在這裏的話題! – 2011-01-27 17:28:08

回答

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>