2017-07-19 74 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时间一致即可。如果你有世界各地的朋友,把测试网站放在某个地方,让他们测试另一个脚本 –