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 10-24-2012, 03:27 PM   PM User | #1
Firedog
New to the CF scene

 
Join Date: Oct 2012
Location: USA East Coast
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Firedog is an unknown quantity at this point
Need help with 3 daily messages rotating indefinitely

I'm sure the answer to this question will seem silly and basic in hindsight, but I just can't get my brain around it. I'd be grateful if someone could help or point me to a script that will accomplish what I'm trying to do.

I trying to come up with a script that will display one of 3 daily messages. Message 1 must display on a given date (for instance Jan. 1, 2012), and then messages 2 and 3 need to display on subsequent days before the cycle starts to repeat (in this case, on January 4, 2012) and then continues to repeat indefinitely into the future.

MANY thanks in advance.
Firedog is offline   Reply With Quote
Old 10-24-2012, 04:00 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
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
Here you are:-
Code:
<span id = "mymessage"</span>

<script type = "text/javascript">

var today = new Date().getTime();
today = Math.floor(today / 86400000) // milliseconds in a day so today = number of days since 01/01/1970

var message1 = "This is message one";
var message2 = "This is message two";
var message3 = "This is message three";

if (today%3 == 0) {document.getElementById("mymessage").innerHTML = message3}
if (today%3 == 1) {document.getElementById("mymessage").innerHTML = message1}    // shows on 1st January 2012
if (today%3 == 2) {document.getElementById("mymessage").innerHTML = message2}

</script>
It was exactly the same score as last season, except is was 0-0 instead of 1-1. - Commentator Sky Sports
__________________

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; 10-24-2012 at 05:36 PM.. Reason: Typo
Philip M is offline   Reply With Quote
Old 10-25-2012, 02:14 AM   PM User | #3
Firedog
New to the CF scene

 
Join Date: Oct 2012
Location: USA East Coast
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Firedog is an unknown quantity at this point
Thanks, Philip M. For some reason, the messages switch at 8 PM instead of at midnight. And actually what I'm looking for (and should have mentioned in the original post) is to have the messages switch at 8 AM. I've played around with the script but can't find anything to make things work correctly. Any thoughts?
Firedog is offline   Reply With Quote
Old 10-25-2012, 03:40 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
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
The change of date occurs at midnight. I don't understand why you say it changes at 8.00pm unless the clock in your computer is wrong.

To change the time when the display changes to the following day at 0800

Code:
var today = new Date().getTime();
today =  today - 28800000; // milliseconds in 8 hours to retard change to 0800
today = Math.floor(today / 86400000) // milliseconds in a day
__________________

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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Firedog (10-25-2012)
Old 10-25-2012, 04:14 AM   PM User | #5
Firedog
New to the CF scene

 
Join Date: Oct 2012
Location: USA East Coast
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Firedog is an unknown quantity at this point
PC time is correct and set via sync with Internet time server. I ended up having to change to

today = today - 43200000

in order to get the message to change at 8 AM, but it works. So weird. I'll check how things appear using some other computers and other browsers but for now things seem OK.

Thank you again.
Firedog is offline   Reply With Quote
Old 10-25-2012, 07:09 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
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 Firedog View Post
So weird.
Are you in a different time zone to GMT? getTime() gets the milliseconds since 01/01/1970 UTC.

It helps when asking questions relating to time to indicate your location in your profile.
__________________

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.
Philip M is offline   Reply With Quote
Old 10-26-2012, 01:57 PM   PM User | #7
Firedog
New to the CF scene

 
Join Date: Oct 2012
Location: USA East Coast
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
Firedog is an unknown quantity at this point
Quote:
Originally Posted by Philip M View Post
Are you in a different time zone to GMT? getTime() gets the milliseconds since 01/01/1970 UTC.

It helps when asking questions relating to time to indicate your location in your profile.
I am in the USA and am on Eastern Time (New York). I will update my profile and I appreciate the suggestion. Is there going to be a problem with the script when going on or off Daylight Saving Time?

One other question since you've been so helpful -- can the color of each of the various rotating messages be different?
Firedog is offline   Reply With Quote
Old 10-26-2012, 04:11 PM   PM User | #8
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,036
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 Firedog View Post
I am in the USA and am on Eastern Time (New York). I will update my profile and I appreciate the suggestion. Is there going to be a problem with the script when going on or off Daylight Saving Time?

One other question since you've been so helpful -- can the color of each of the various rotating messages be different?
You will need to take account of DST in your timezone.

if (today's date is between the start of DST and end of DST) {add one hour}

Of course the color of the messages can be different - use css to style them.

Code:
if (today%3 == 0) {document.getElementById("mymessage").innerHTML = message3; 
document.getElementById("mymessage").style.color = "blue"}
__________________

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.
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:34 AM.


Advertisement
Log in to turn off these ads.