2017-07-19 40 views
0

Stackoverflow社區。設置時間特定的循環

我對編碼相對比較陌生,JS很特別,我目前正在爲無線電臺工作。我正在寫JS,根據目前播出的主持人更改橫幅圖像。爲了成功,我需要每小時(上午9點或下午4點)和每半小時(4點半或13點半)運行腳本,無論用戶何時加載頁面。

這是目前使用的代碼IM

[JS的剪斷] [1]:https://i.stack.imgur.com/WgvnX.jpg

我有一個調用函數的第一次,那麼我想它設置超時運行本身再次在下半個小時的時間間隔,然後繼續循環直到用戶關閉瀏覽器。

謝謝你的答案提前

+0

https://stackoverflow.com/questions/ 26306090/running-a-function-everyday-midnight,https://stackoverflow.com/questions/4455282/call-a-javascript-function-at-a-specific-time-of-day,https:// stackoverflow。每天運行一次的代碼,https://stackoverflow.com/questions/11901074/javascript-call-a-function-after-specific-time-period – Morpheus

+0

只是要清楚,你客戶端每半小時不需要運行任何腳本。你的劇本必須提供一組記者,每個記者旁邊有一段時間(比如電視指南)。然後一個客戶端瀏覽器的setTimeout剛剛從該陣列中讀取數據,該記錄位於 –

+0

@EmmanuelDelay您所看到的剪切函數調用函數來查看它是哪周的哪一天。該功能然後調用函數來查看當天的時間,並傳遞正確的圖像以在瀏覽器中顯示。希望清除它? –

回答

0
nextDate = new Date(); 
bVal = d.getMinutes() > 30 
d.setHours(d.getHours() + 1 * bVal,30 ? bVal : 0) 

這應該設置你的時間是正確的值

+0

謝謝Tomos,只是一件事。我是否必須更改我的代碼或只添加該行?我會測試和更新,如果它工作 –

+0

Tomos它會出現你的代碼工作得很好,但它已經揭示了一個新問題。我會在解決問題時添加一個答案來顯示我的新問題。 –

+0

@JHerselman目前還看不到新的問題,當您添加它並嘗試修復它時,請在此處註釋,我會盡力幫助我儘可能地幫到您 –

0

如果下面的代碼運行,所以如果時間大於30分鐘,這樣看來,過去,它將超時設置爲1小時後而不是下一個小時。換句話說,它現在是下午1點42分,它已經設定超時時間在下午2點30分開始。

[新問題]:https://i.stack.imgur.com/iAUnY.jpg

+0

編輯您的問題,而不是用新的問題發佈答案問題。 – Morpheus

1

這是一個非常簡單的實現。

編輯:這是與時區,但我不是100%確定它的工作原理,因爲它應該。時區可能會非常棘手。

<script> 
var currentIndex = 0; // global var. If the reporter index is different than this, the change will be triggered. 

var serverTimezone = -4 // New York Timezone. notice DST. In Winter time this should be -5. 

// all on New York Time 
var reporters = [ 
    {name: 'Tintin',  time: '2017-07-19 08:00:00', url: 'http://photos1.blogger.com/img/28/3438/320/Tintin.jpg'}, 
    {name: 'Kent Brockman', time: '2017-07-19 09:00:00', url: 'https://static.simpsonswiki.com/images/thumb/1/16/Kent_Brockman.png/250px-Kent_Brockman.png'}, 
    {name: 'Clark Kent', time: '2017-07-19 11:00:00', url: 'https://i.ytimg.com/vi/9HbXdHu3NaI/hqdefault.jpg'}, 
    {name: 'Peter Parker', time: '2017-07-19 13:00:00', url: 'http://sotd.us/justingabrie/peterparker/Module08/images/peterparker.jpg'} 
]; 

function getReporter() { 
    var index = 0; 
    var now = new Date(); 
    var clientTimezone = -1 * now.getTimezoneOffset()/60; // this returns (in Summer time) example: for Brussels: 2 , NYC: -4, ... 
    var timezoneOffset = serverTimezone - clientTimezone; 
    for(var i=0; i< reporters.length; i++) { 
    var reporter_time = parseDatetimeString(reporters[i].time, timezoneOffset); 
    if(now >= reporter_time) { 
     index = i; 
    } 
    } 
    if(currentIndex != index) { 
    changeReporter(index); 
    } 
} 

function changeReporter(index) { 
    currentIndex = index; 
    document.getElementById('banner').src = reporters[index].url; 
} 

function parseDatetimeString(s, timezoneOffset) { 
    if(! timezoneOffset) { 
    timezoneOffset = 0; 
    } 
    var bits = s.split(/\D/); 
    return new Date(bits[0], --bits[1], bits[2], Number(bits[3]) - timezoneOffset, bits[4], bits[5]); 
} 

window.onload = function() { 
    getReporter(); 
    // check every 20 sec, feel free to change this value, 
    // it isn't harmful to the client so set it even smaller 
    setInterval(getReporter, 20000); 
} 
</script> 
<img id="banner"/> 

時區尚未包括在內;你在哪個時區?有一些工作來處理這個問題。

(我在肯特·布羅克曼現在,14:00:布魯塞爾時間00H,但想必你在其他時區...客戶端)

<script> 
var currentIndex = 0; // global var. If the reporter index is different than this, the change will be triggered. 
var reporters = [ 
    {name: 'Tintin',  time: '2017-07-19 13:00:00', url: 'http://photos1.blogger.com/img/28/3438/320/Tintin.jpg'}, 
    {name: 'Kent Brockman', time: '2017-07-19 14:00:00', url: 'https://static.simpsonswiki.com/images/thumb/1/16/Kent_Brockman.png/250px-Kent_Brockman.png'}, 
    {name: 'Clark Kent', time: '2017-07-19 15:00:00', url: 'https://i.ytimg.com/vi/9HbXdHu3NaI/hqdefault.jpg'}, 
    {name: 'Peter Parker', time: '2017-07-19 16:00:00', url: 'http://sotd.us/justingabrie/peterparker/Module08/images/peterparker.jpg'} 
]; 

function getReporter() { 
    var index = 0; 
    var now = new Date(); 
    for(var i=0; i< reporters.length; i++) { 
    var reporter_time = parseDatetimeString(reporters[i].time); 
    if(now >= reporter_time) { 
     index = i; 
    } 
    } 
    if(currentIndex != index) { 
    changeReporter(index); 
    } 
} 

function changeReporter(index) { 
    currentIndex = index; 
    document.getElementById('banner').src = reporters[index].url; 
} 

function parseDatetimeString(s) { 
    var bits = s.split(/\D/); 
    return new Date(bits[0], --bits[1], bits[2], bits[3], bits[4]); 
} 

window.onload = function() { 
    getReporter(); 
    // check every 20 sec, feel free to change this value, 
    // it isn't harmful to the client to set it even smaller 
    setInterval(getReporter, 20000); 
} 
</script> 
<img id="banner"/> 
+0

我在格林威治標準時間+2。我不知道哪個時區服務器尚未開啓。 –

+0

沒有時區的腳本應該在西歐布魯塞爾時間(不在英國)工作得很好。只要陣列中的時間與GMT + 2時間一致即可。如果你有世界各地的朋友,把測試網站放在某個地方,讓他們測試另一個腳本 –

相關問題