0

I have a simple code that shows one image at night and one image during the day. For some reason the day image always shows.

The javascript is a bit split. This part is on the page:

<script>

//waits until document is open

$(window).load(function() {

//if it's night time when you read this it displays a night time photo, if it's day time it's a day time photo... don't know how many people might notice

if (time === "night"){
 $.backstretch("cafenight.jpg");

 //makes the font white with a black outline so you can read it easily on the night photo
  $("#content").css("color","#FFFFFF");
    $("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0     #000");

}

else {
 $.backstretch("cafeday.jpg");

    $("#content").css("text-shadow","1px 0 0 #fff, 0 -1px 0 #fff, 0 1px 0 #fff, -1px 0 0      Ωfff");}});

 $(document).ready(function(){

 //fades in the first line of text

$("#partFive").fadeIn(2000)

setTimeout(function () {

//fades in the second line of text

$("#partFive").fadeOut(2000)

setTimeout(function () {


$("#partSix").fadeIn(3000)

setTimeout(function () {


$("#partSix").fadeOut(3000)


setTimeout(function () {

window.location.href = "6.html";

    }, 4000);

    }, 2000);

    }, 2000);

    }, 3000);

});

</script>

This part is in basic2.js

// Javascript and Jquery rock!
//Variables to be used throughout
//The user's current hour

var hour = (new Date()).getHours()

//use the variable 'time' to know if it is day or night. Can use to change background even wording.
//The variable 'time' will either be Day or Night depending on what time it is

var time = []

if (hour < 20){
 time.push("day");

}
else {
time.push("night");

}


//Gets the user's city based on IP and replaces #homeCity span with the name of their city
// not perfectly accurate but fun


$.getJSON("http://www.telize.com/geoip?callback=?",
    function(json) {

        $("#homeCity").html(json.city);

    }
    );


//The user's username, they enter it near the beginning

var userName = []
5
  • what is the value of time when u hit the if statement in the first file? Commented Jul 18, 2014 at 14:22
  • 1
    time is an array which might contain a string "night". if (time === "night") will always return false since time is not a string. Commented Jul 18, 2014 at 14:24
  • It might help if you could set up a jsfiddle Commented Jul 18, 2014 at 14:25
  • Hi John, I think you might be right. How do I set it correctly? Still new to javascript, this is all part of the teething process for me. Commented Jul 18, 2014 at 14:26
  • Where in the page is basic2.js loaded? Before or after the <script> tag code you showed? Moreover, I am just curious on the motivation to use an array for "time" and not just a plain variable. Commented Jul 18, 2014 at 14:27

2 Answers 2

3

I think you have made a mistake with types. You define time as an array and push into it... but in the other file you refer to it as a string and do a type and value comparison on it. It will never equal "night" on this line so will always go with else and therefore default to the day image.

Edit:

var isDay = (hour < 20);

if (isDay) {
//do day stuff
}else{
//do night stuff
}
Sign up to request clarification or add additional context in comments.

2 Comments

That sounds about right, still new to javascript. What would be the best solution to do what I would like?
Get rid of the array. Use a boolean variable instead.
0

It looks like your time variable is an array

var time = []

if (hour < 20){
 time.push("day");

}

else {
time.push("night");

}

Yet in your if statement it's being treated like a string

if (time === "night"){
    $.backstretch("cafenight.jpg");

    //makes the font white with a black outline so you can read it easily on the night photo
    $("#content").css("color","#FFFFFF");
    $("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0     #000");



}

Changing the if statement should do the trick

if (time[0] === "night") {
    ...
}

Edit: Otherwise, use a boolean as suggested in the other comment

var day;

if (hour < 20){
 day = true;

}

else {
    day = false

}

then

if (!day){
    $.backstretch("cafenight.jpg");

    //makes the font white with a black outline so you can read it easily on the night photo
    $("#content").css("color","#FFFFFF");
    $("#content").css("text-shadow","1px 0 0 #000, 0 -1px 0 #000, 0 1px 0 #000, -1px 0 0     #000");
}

2 Comments

Will do as you say and give it a test. Thanks SO much.
Just made the if statement grab the array as suggested and it worked! yay!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.