Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-22-2013, 09:22 AM   PM User | #1
davidwhite
New Coder

 
Join Date: Nov 2011
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
davidwhite is an unknown quantity at this point
Script to change image at same time every day

Hello,

I need a script (must be in JavaScript) that changes an image at the same time every day of the week.

Let's say it changes at 8am every day.

I presume it would hold the 7 images in an array and would use something like Date.UTC to ensure everyone was seeing the same image? (Assuming people have their times set right).

Is this something that's hard to do? I have tried to modify existing scripts but can't get anything to work.

Any help appreciated.

Thanks
davidwhite is offline   Reply With Quote
Old 01-22-2013, 10:51 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Yes, terribly hard.

Code:
<img id = "myimage" src = "">

<script type = "text/javascript">

// change image at 0800 daily according to user's time

var pics = [];
pics[0] = "Sunday.jpg";
pics[1] = "Monday.jpg";
pics[2] = "Tuesday.jpg";
pics[3] = "Wednesday.jpg";
pics[4] = "Thursday.jpg";
pics[5] = "Friday.jpg";
pics[6] = "Saturday.jpg"

var d = new Date();   // today
var hr = 8;  // hours to subtract (or add)
d = d.getTime() - (3600000*hr);  // subtract hr hours
var nd = new Date(d);
var dy = nd.getDay();
document.getElementById("myimage").src = pics[dy];

</script>

You say you want all users to see the same image, changing at 0800 local time. Obviously that is not the same thing as all users seeing the same image at all times. Or do you want to change the image at 0800 GMT, regardless of the user's local time?


It wasn't funny but it did make me laugh. - Reporter, Surrey Advertiser.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-22-2013 at 10:58 AM..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
davidwhite (01-22-2013)
Old 01-22-2013, 11:44 AM   PM User | #3
davidwhite
New Coder

 
Join Date: Nov 2011
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
davidwhite is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Yes, terribly hard.

Code:
<img id = "myimage" src = "">

<script type = "text/javascript">

// change image at 0800 daily according to user's time

var pics = [];
pics[0] = "Sunday.jpg";
pics[1] = "Monday.jpg";
pics[2] = "Tuesday.jpg";
pics[3] = "Wednesday.jpg";
pics[4] = "Thursday.jpg";
pics[5] = "Friday.jpg";
pics[6] = "Saturday.jpg"

var d = new Date();   // today
var hr = 8;  // hours to subtract (or add)
d = d.getTime() - (3600000*hr);  // subtract hr hours
var nd = new Date(d);
var dy = nd.getDay();
document.getElementById("myimage").src = pics[dy];

</script>

You say you want all users to see the same image, changing at 0800 local time. Obviously that is not the same thing as all users seeing the same image at all times. Or do you want to change the image at 0800 GMT, regardless of the user's local time?


It wasn't funny but it did make me laugh. - Reporter, Surrey Advertiser.
Thanks for the help.

To be honest, thinking about it, it's not that big a deal that people are in different time zones. So long as it changes once a day for everyone then that's fine.

Let's just keep it simple and assume it changes 8.00am UTC for everyone, no matter what their local time is.
davidwhite is offline   Reply With Quote
Old 01-22-2013, 01:46 PM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,037
Thanks: 197
Thanked 2,411 Times in 2,389 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by davidwhite View Post
Let's just keep it simple and assume it changes 8.00am UTC for everyone, no matter what their local time is.
It would seem to be better to change at 0800 local time for everyone, but if for some reason every user must see the same image (why?) then why change at 0800 rather than midnight UTC?

<img id = "myimage" src = "">

<script type = "text/javascript">

// change image at 0800 UTC daily

var pics = [];
pics[0] = "Sunday.jpg";
pics[1] = "Monday.jpg";
pics[2] = "Tuesday.jpg";
pics[3] = "Wednesday.jpg";
pics[4] = "Thursday.jpg";
pics[5] = "Friday.jpg";
pics[6] = "Saturday.jpg"

var d = new Date(); // today
var hr = 8; // hours to subtract (or add)
var off = d.getTimezoneOffset(); // offset in minutes
off = off *60*1000; // offset in milliseconds
d = d.getTime() - (3600000*hr) - off; // subtract hr hours and timezone offset
var nd = new Date(d);
var dy = nd.getDay();
document.getElementById("myimage").src = pics[dy];

</script>

Note that Javascript relies on the accuracy of the user's computer clock. If it is important that all users always see the same image then you need server-side scripting.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 01-22-2013 at 05:09 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:46 AM.


Advertisement
Log in to turn off these ads.