这是一个非常简单的实现。
编辑:这是与时区,但我不是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"/>
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
只是要清楚,你客户端每半小时不需要运行任何脚本。你的剧本必须提供一组记者,每个记者旁边有一段时间(比如电视指南)。然后一个客户端浏览器的setTimeout刚刚从该阵列中读取数据,该记录位于 –
@EmmanuelDelay您所看到的剪切函数调用函数来查看它是哪周的哪一天。该功能然后调用函数来查看当天的时间,并传递正确的图像以在浏览器中显示。希望清除它? –