JSFiddle - React, Tailwind, and code Playground

by sazakharov

JavaScript

function changeBackground(){
	var h = (new Date()).getHours();
  var b = document.body; 
  if (h > 0 && h < 6) {
  	// ночь
  	img = 'https://images.wallpaperscraft.com/image/moon_branch_night_clouds_48105_1920x1080.jpg';
  } else if (h >=6 && h < 10) {
  	// утро
  	img = 'https://i.artfile.me/wallpaper/11-09-2012/1920x1080/priroda-voshody-zakaty-lug-voshod-utro-t-660656.jpg';
  } else if (h >= 10 && h < 19) {
  	// день
    img = 'https://get.wallhere.com/photo/sunlight-nature-grass-sky-field-horizon-cloud-grassland-meadow-plain-1920x1080-px-prairie-computer-wallpaper-cumulus-573793.jpg';
  } else if (h >= 19) {
  	// вечер
    img = 'https://www.bmwunstoppable.com/wp-content/uploads/2017/03/5nXkqs.jpg';
  }
  b.style = 'background-image: url('+img+')';
  setTimeout(changeBackground, 10000);
}

setTimeout(changeBackground, 1000);