Skip to content Skip to sidebar Skip to footer

How To Change Background Image By Time Of Day Without Page-refresh

What I'm looking to achieve is having the background image of my page change based on the hour of the day. This is fairly easy, as it only requires retrieving the hour through the

Solution 1:

Just check the current background URL before updating it?

setInterval(function () {
     var hours = new Date().getHours();
     $("#hours").html((hours < 10 ? "0" : "") + hours);

     if (hours > 9 && hours < 18) {
          if($("body").css("background-image").indexOf("night-sky-background.jpg")==-1){
              $("body").css("background-image", "url(../images/night-sky-background.jpg)");
          }
     } else {
          if($("body").css("background-image").indexOf("day-sky-background.jpg")==-1){
             $("body").css("background-image", "url(../images/day-sky-background.jpg)");
          }
     }
}, 1000);

Post a Comment for "How To Change Background Image By Time Of Day Without Page-refresh"