This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (41 loc) · 1.41 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
var owmApiKey = ''
var baseUrl = 'http://api.openweathermap.org/data/2.5/weather?'
var html = document.querySelector('html')
var clock = document.querySelector('.clock')
var date = document.querySelector('.date')
var weather = document.querySelector('.weather')
moment.locale('en-gb')
init()
function init () {
updateBackground()
setInterval(updateBackground, 86400000) // 24 hrs
updateDisplay()
setInterval(updateDisplay, 3000) // 3 sec
updateWeather()
setInterval(updateWeather, 1800000) // 30 min
}
function updateDisplay () {
clock.innerHTML = moment().format('h:mm')
date.innerHTML = moment().format('dddd') + ', ' + moment().format('LL')
}
function updateBackground () {
fetch('https://www.reddit.com/r/EarthPorn/.json')
.then(r => r.json())
.then(function (data) {
var random = Math.floor(Math.random()*10)
var imgUrl = data.data.children[random].data.preview.images[0].source.url
var darken = 'linear-gradient( rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4) )'
var bgImg = 'url("' + imgUrl + '")'
document.body.style.backgroundImage = darken + ', ' + bgImg
})
.catch(e => console.log(e))
}
function updateWeather () {
fetch(baseUrl + 'id=2207616&appid=' + owmApiKey)
.then(r => r.json())
.then(function (data) {
console.log(data)
weather.innerHTML = Math.round(data.main.temp - 273.15) + ' Celsius'
})
.catch(function (e) { console.log(e) })
}