-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
70 lines (53 loc) · 2.26 KB
/
weather.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
function apiRequest() {
//create new XMLHttpRequest
var request = new XMLHttpRequest();
// open a new conection using get
request.open('get', 'http://api.openweathermap.org/data/2.5/weather?id=524901&APPID=1de0167063f9f2c63e4aff1f18c1e720&zip=53151,us&units=imperial', true);
//directions for what to do on load of request
request.onload = function() {
//if successful parse data
if (request.status === 200) {
var data = JSON.parse(this.response);
//store parsed data in variables
var city = data.name;
var temp = Math.round(data.main.temp) + ' F';
var weather = data.weather[0].main;
var icon = '<img src="weather-icons/' + data.weather[0].icon + '.svg" alt="' + data.weather[0].icon + '">';
//build divs to house data and set their class names
//Place the stored data inside of div
const cityDiv = document.createElement('div');
cityDiv.setAttribute('class', 'city');
cityDiv.textContent = city;
const tempDiv = document.createElement('div');
tempDiv.setAttribute('class', 'temp');
tempDiv.textContent = temp;
const weatherDiv = document.createElement('div');
weatherDiv.setAttribute('class', 'weather');
weatherDiv.textContent = weather;
const iconDiv = document.createElement('div');
iconDiv.setAttribute('id', 'icon');
iconDiv.innerHTML = icon;
//grabs container to paste new divs into
const container = document.getElementById('weather-module-content');
const iconContainer = document.getElementById('weather-module');
// Delete contents of containing divs
//Must happen here to avoid DOM flicker on reload
document.getElementById('icon').parentNode.removeChild(document.getElementById('icon'));
container.textContent = "";
//append divs in appropriate places
iconContainer.insertBefore(iconDiv, container);
container.appendChild(cityDiv);
container.appendChild(tempDiv);
container.appendChild(weatherDiv);
} else {
console.log('Error');
}
}//end load
request.send();
//Loops the function every Minute
var timeOut = setTimeout(function() {
apiRequest();
},60*1000)
}//end apiRequest()
//Calls apiRequest()
apiRequest();