Reputation: 91
I have a code trying to get Barcelona city hour to show me a specific background and image too (based on the hour of the day) but seems to be that's not working.
JAVASCRIPT CODE
var now = new Date();
var offset = now.getTimezoneOffset();
var barcelona = new Date(now + (offset + 2*60)*60*1000);
var n = barcelona.getHours();
//1-2am if (n > 23 || n < 2) {
document.write('<body bgcolor="#2e3348" text="#FFFFFF">');
$("img#photo1").attr("src","images/head5.png");
}
//2-3am if (n > 24 || n < 4) {
document.write('<body bgcolor="#2e3348" text="#FFFFFF">');
$("img#photo1").attr("src","images/head4.png");
}
// and so go on - all the hours
And This is my HTML code
<div class="Center-child-div">
<div class="child-div">
<img src="" id="photo1" />
</div>
</div>
Any explanation why's not changing the background, is not showing the respective .png image? possible solution? thanks
Upvotes: 0
Views: 94
Reputation: 24276
Try &&
operator instead of ||
. Also pay more attention to the if
conditions:
var now = new Date(),
offset = now.getTimezoneOffset(),
barcelona = new Date(now + (offset + 2*60)*60*1000),
n = barcelona.getHours();
// 1-2am
if (n > 24 && n < 3) {
$('body').css({
'background-color': '#2e3348',
'color': '#fff'
});
$("img#photo1").attr("src","images/head5.png");
} else
// 2-3am
if (n > 1 && n < 4) {
$('body').css({
'background-color': '#2e3348',
'color': '#fff'
});
$("img#photo1").attr("src","images/head4.png");
} else
// ......... if
Upvotes: 2
Reputation: 763
I ran your code. The problem was that you had too many closing brackets. Next time, you can just open your console log, and it will give you any Javascript errors that are occurring. If Javscript encounter into an error, it will stop running at that point and any code after said point will not run.
This is the code that I used and had it work.
var now = new Date();
var offset = now.getTimezoneOffset();
var barcelona = new Date(now + (offset + 2*60)*60*1000);
var n = barcelona.getHours();
//1-2am if (n > 23 || n < 2) {
document.write('<body bgcolor="#2e3348" text="#FFFFFF">');
$("img#photo1").attr("src","images/head5.png");
//2-3am if (n > 24 || n < 4) {
document.write('<body bgcolor="#2e3348" text="#FFFFFF">');
$("img#photo1").attr("src","images/head4.png");
Also, I'm not sure if you include jQuery somewhere, but you can't use the $
reference without it.
Upvotes: 0