Forum Moderators: open

Message Too Old, No Replies

serving different content round the clock

         

Oddjob41

7:56 am on Jun 26, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



Hi,

I've written a function which is meant to serve different content 24/7, changing every hour. It doesn't work, and I'm not getting error messages. It compares date objects, as in this thread (scroll down):
[stackoverflow.com...]

Maybe someone can help?

As a test, the function changes the background-colour of one element from red to green (NB, this uses jQuery, but this doesn't appear to be part of the problem).

var newhour = new Date();
var midnite = new Date('1980-01-01 00:00');
var oneam = new Date('1980-01-01 01:00');
var twoam = new Date('1980-01-01 02:00');
// thru to...
var ninpm = new Date('1980-01-01 21:00');
var tenpm = new Date('1980-01-01 22:00');
var elvpm = new Date('1980-01-01 23:00');

function bigClock() {
if ( newhour >= midnite && newhour < oneam ){ doMidnite() }
if ( newhour >= oneam && newhour < twoam ){ doOneam() }
if ( newhour >= twoam && newhour < thram ){ doTwoam() }
// thru to...
if ( newhour >= ninpm && newhour < tenpm ){ doNinepm() }
if ( newhour >= tenpm && newhour < elvpm ){ doTenpm() }
if ( newhour >= elvpm && newhour < midnite ){ doElevenpm() }
}

function doMidnite() { $('#colbg').css( 'background-color', 'red') }
function doOneam() { $('#colbg').css( 'background-color', 'green') }
function doTwoam() { $('#colbg').css( 'background-color', 'red') }
//
function doNinepm() { $('#colbg').css( 'background-color', 'green') }
function doTenpm(){ $('#colbg').css( 'background-color', 'red') }
function doElevenpm(){$('#colbg').css( 'background-color', 'green') }


The whole shebang called with bigClock().

Cheers...

keyplyr

10:02 am on Jun 26, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not getting error messages
Can't help with the code but have you tried the Chrome browser Dev tools? Or Firefox also has tools for finding errors on a page.

NickMNS

2:46 pm on Jun 26, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



My guess is that the problem lies with
Date('1980-01-01 00:00');


You are using a date to compare a time. Any current time you choose will always be greater than the time set in your vars. The reason you don't get an error is that the code is technically okay, its is the logic that needs improvement.

LifeinAsia

3:56 pm on Jun 26, 2018 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The SO thread you linked to gave you exactly the answer you needed. Use the .getHours() JavaScript function to get the hour component of the current time (of your user) and then build your if logic around the resulting number between 0 and 23.

Oddjob41

9:06 pm on Jun 26, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



Many thanks! can do.
Cheers!