Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 2 of 2 FirstFirst 12
Results 16 to 29 of 29
  1. #16
    rgy
    rgy is offline
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I did alter the code so that I could use a <script> tag to post it and post just the information I wanted instead of the picture and title all at once. I have .js files for the picture then the title so I can do different things with them - that's a different story. One of my files looks like this:

    Code:
    var GMToffset = -6; // Your current GMT offset, whether Standard or Daylight 
    var now = new Date();
    now.setHours(GMToffset + now.getHours() + now.getTimezoneOffset() / 60);
    var hours = now.getHours();
    var mn = now.getMinutes();
    hours = hours + (mn/60);
    
    //Configure message below to your own.
    if (hours>=1&&hours<=2.25) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/rabbitanddavid2.bmp"/>')
    else if (hours>=3&&hours<=4.25) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/sad2.bmp"/>')
    else if (hours>=6&&hours<=7) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/morning2.bmp"/>')
    else if (hours>=12&&hours<=13) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/featuredshow2.bmp"/>')
    else if (hours>=15&&hours<=16.50) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/passtime2.bmp"/>')
    else if (hours>=18&&hours<=20) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/passtime2.bmp"/>')
    else if (hours>=20&&hours<=21.50) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/passtime2.bmp"/>')
    else if (hours>=21.50&&hours<=23) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/playlist2.bmp"/>')
    else if (hours>=23&&hours<=0.50) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/davidshow2.bmp"/>')
    else if (hours>=0.50&&hours<=0.75) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/rockiesrecap2.bmp"/>')
    else //MESSAGE FOR LATE NIGHT, EARLY MORNING (12pm-4am)
    document.write('<img src="/images/playlist2.bmp"/>')
    This code has been working very well for the time I have been using it until it suddenly stopped working during the 11pm hour. I'm not sure why. I have been through it to make sure I don't have extra or stray punctuation such as; ' marks that might alter the way the code works and I can find no errors. It only started doing this after I changed 12am from 24 to 0. Now the 12am hour works but the 23 does not. I have also tried elliminating the 0 and doing different time frames that have been working; i.e.: 23 - 1 and the 23 hour still does not work. I'm sure it's just a simple solution that I am not seeing to my unprofessional eye.

  2. #17
    rgy
    rgy is offline
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want to add that I just copied the lines when I wanted to add another picture for another show so pretty much all of the lines have "//MESSAGE FOR AFTERNOON" after it but that doesn't or shouldn't affect the way the code works in any way, I just never deleted these messages.

  3. #18
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    else if (hours>=23 && hours<=0.50)

    The hours value cannot be both greater than 23 and less than 0.5.

    Change this line to

    else if (hours>=23 || hours<=0.50)

    Can you see why it would work in the 23 hour if the hours value was 24.5 (which of course can never occur)? Comparison using && requires that the second of the two numbers is greater than the first.

    Although it is not a fatal error here you have assigned the same time to two conditions, e.g.

    else if (hours>=21.50&&hours<=23) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/playlist2.bmp"/>')
    else if (hours>=23&&hours<=0.50) //MESSAGE FOR AFTERNOON
    document.write('<img src="/images/davidshow2.bmp"/>')

    You should preferably assign 23 hours to one or the other.

    You have omitted the braces { } around your if statements. That is allowable if and only if only one line of code follows. It is very poor practice as changes or additions to the code may well foobar it.
    Last edited by Philip M; 04-16-2011 at 07:46 AM.

  4. #19
    rgy
    rgy is offline
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oh haha, rookie mistake, right? lol
    It makes sense when it's explained. Otherwise i'm on my own scratching my head and changing the code around until NOTHING works.
    That does make sense that when using && 0.50 would have to be greater than 23 - it's obviously not. So, for 23 on two conditions, will that cause it to not work correctly? I thought it would be ok because I was thinking about it and on one condition it is using less than 23 <23, and the other greater than 23 >23, I thought those were two different things. On a script like this just including 23 on two scripts will mess it up?
    As far as brackets go, do I need a set before the "else" and at the end of the document line of each condition?
    I know you said it's not a fatal error, so does that mean it will not cause the script to not work? Will the pictures still change at or a minute after the 23 hour?
    Thanks again!
    Last edited by rgy; 04-17-2011 at 05:16 AM.

  5. #20
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Hint: Because you're using the expression
    Code:
    hours = hours + (mn/60);
    you will need to be very careful about rounding errors.

    (Looks like you'll be up til midnight )

  6. #21
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You probably don't want to modify your code but, if it were me, I think I would prefer to keep the hours and minutes separate:

    Code:
    var hours = now.getHours();
    var mn = now.getMinutes();
    // hours = hours + (mn/60); - remove this
    
    if (hours>=1&&hours<=2.25) //MESSAGE FOR AFTERNOON
    if (hours==1 || (hours==2 && mn<=15)) // alternative version
    // believe this is right?
    This avoids any concerns about rounding and allows you to check for equality. But, as I say, you probably don't want to change it now.

  7. #22
    rgy
    rgy is offline
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok. That's pretty neat. So, the thing that confuses me about that alternative version is all the parenthesis ( ), what would it look like if I had minutes on both start and end hours?

    if ((hours==20 && mn=<15) || (hours==20 && mn<=30))
    document.write('<img src="/images/playlist2.bmp"/>')

    ???? - the one above doesn't seem to work lol I'm sure I screwed that all up, too many parenthesis maybe...

  8. #23
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    Quote Originally Posted by rgy View Post
    Ok. That's pretty neat. So, the thing that confuses me about that alternative version is all the parenthesis ( ), what would it look like if I had minutes on both start and end hours?

    if ((hours==20 && mn=<15) || (hours==20 && mn<=30))
    document.write('<img src="/images/playlist2.bmp"/>')

    ???? - the one above doesn't seem to work lol I'm sure I screwed that all up, too many parenthesis maybe...
    CAREFULLY re-read his suggestion of post #21.

  9. #24
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by AndrewGSW View Post
    Hint: Because you're using the expression
    Code:
    hours = hours + (mn/60);
    you will need to be very careful about rounding errors.

    (Looks like you'll be up til midnight )
    Why? Explain please? My code turns minutes into fractions of an hour to avoid the malarky you propose.
    And extreme accuracy to the second is not required for the display of a message at a certain time.

  10. #25
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Well extreme accuracy is probably not such a concern with this example, although awareness of rounding errors is important.

    I just prefer the malarky of separating the hours and minutes. I know that 'mn == 20' means it's twenty past, rather than

    Code:
    (hours - parseInt(hours)) >= 0.3 possibly && 
    (hours - parseInt(hours)) < 0.33333333333333334 possibly

  11. #26
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    .3334 of an hour is 20.004 minutes. The error is 0.24 seconds. Bearing in mind that the time is taken from the user's clock which could be seconds or minutes out, there is little point in attempting any additional accuracy.

    In fact the times are at 15, 30 or 45 minutes where no rounding takes place.

    Rounding errors are indeed important is some situations. But this is not one of them.

  12. #27
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    I wondered when you might come back to me

    I did not know that ".3334 of an hour is 20.004 minutes".

    I shall retain my preference for distinguishing between hours and minutes.

  13. #28
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Quote Originally Posted by AndrewGSW View Post
    I shall retain my preference for distinguishing between hours and minutes.
    That is perfectly OK. As we have said many times before in this forum, there are several different ways to skin a cat. If you want to do something the hard way that is fine by me.

    You are quite free to get the value of a selected option with
    document.formname.selectlistname.options[document.formname.selectlistname.selectedIndex].value
    if you want.

    Once again - bearing in mind that the time is taken from the user's clock which could be seconds or minutes out, there is little point in attempting any additional accuracy in client-side timers in any situation.

  14. #29
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Okay.


 
Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •