I am trying to display some content for a two hour slot, for example 1.00pm - 3.00pm.
I have this code:
//initialize date object
var d = new Date();
var currentHour = d.getHours(); //note 0-23
if (currentHour < 1)
{
$('div').append('hello');
}
else if (currentHour == 3)
{
$('div').append('content shown for two hours');
}
else {
$('div').append('hello');
}
Is there a way I can modify the above to show the content for two hours?
window.setInterval(function() {
var d = new Date();
if (d.getHours() > 12 && d.getHours() < 15) { // if it's after 1pm but before 3pm
$('div').show();
} else {
$('div').hide();
}
}, 1000 * 60) // this will run every minute
This uses setInterval to run a check every minute and hide or show the content accordingly.
Anywhere on your page. Also include a div with an id of “clock” to see a groovy clock.
I might point out a couple of things:
If the user visits your page and the time is between 13:00 and 15:00, the script won’t fire.
Also, AFAIK, a JavaScript function name cannot start with a number.
Ah right, I was wondering where the AJAX part came into things.
So, if I understand you correctly, you keep track of the time, then when the time is between two specified points, you load the content into a pre-defined div.
Did I understand that right?