Update - Already caught one issue. Was simply my stupid mistake. How do I add a function to make this code rerun itself for each time block, so it updates on an open browser? - Thanks
I am trying to have a simple gallery of images (For example 20 images, 15 sec each, scrolling indefinitely), but all the images will change based on the time of day. For example: 8am-11am one set, 12pm-4pm another, 4-8pm, and 8pm-7am. I have been messing with this from existing code I have found, and tried a ton of versions, but none have worked.
This is the code idea I am messing with (not working), but maybe I should be going about this entirely differently. Thoughts?
(example of different time blocks, and only with 3 images - but doesn't work)
$(document).ready(function(){
var d = new Date();
var n = d.getHours();
// If time is after 8PM or before 7AM
if (n > 20 || n < 7) {
$("img#photo1").attr("src","img/8TO7/photo1.jpg");
$("img#photo2").attr("src","img/8TO7/photo2.jpg");
$("img#photo3").attr("src","img/8TO7/photo3.jpg");
}
else if (n > 16 && n < 20) {
$("img#photo1").attr("src","img/4TO8/photo1.jpg");
$("img#photo2").attr("src","img/4TO8/photo2.jpg");
$("img#photo3").attr("src","img/4TO8/photo3.jpg");
}
else {
$("img#photo1").attr("src","img/main/photo1.jpg");
$("img#photo2").attr("src","img/main/photo2.jpg");
$("img#photo3").attr("src","img/main/photo3.jpg");
}
});
The following demo should do what you want.
You can re-run your code with
setIntervall(callback, time_in_ms)
. In the demo the interval is set to 5sec. to show that it is running.Maybe you could add image caching and a check that the images are only reloaded if necessary. At the moment, it reloads the images at every callback.
You can find the same code here at jsFiddle.
For testing you can pass a time to
var d = new Date('17/12/2014 16:00:00');
so it's easier to test that the right set is displayed.