-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyapp.js
60 lines (43 loc) · 1.77 KB
/
myapp.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
"use strict";
var searchButton = document.querySelector("button");
var searchCity = document.querySelector('#city');
searchButton.addEventListener('click', searchWeather);
let load = document.querySelector("#load");
let weatherBox = document.querySelector("#weather");
let weatherCity = weatherBox.firstElementChild;
let weatherDescription = document.querySelector('#weatherDescription');
let weatherTemperature = weatherBox.lastElementChild;
function searchWeather() {
weatherBox.style.display="none";
load.style.display='block';
var cityName = searchCity.value;
if (cityName.trim().length === 0) {
return alert("Please enter the City Name")
}
let http = new XMLHttpRequest();
var apiKey = '059cf2b6f2741f27e2b1644ca23d139d';
http.open('GET', 'http://api.openweathermap.org/data/2.5/weather?q=' + cityName + '&units=metric&apiKey=' + apiKey);
http.onload = function () {
if (http.readyState === XMLHttpRequest.DONE && http.status === 200) {
let data = JSON.parse(http.responseText);
let description = data.weather[0].description.toUpperCase();
let temp = data.main.temp;
let weather = new Weather(cityName, description);
weather.temperature = temp;
console.log(weather);
setTimeout(function () {
updateWeather(weather);
},2000);
} else if (http.readyState === XMLHttpRequest.DONE) {
alert('Something went wrong')
}
};
http.send()
}
function updateWeather(weather) {
weatherCity.textContent = weather.cityName;
weatherDescription.textContent=weather.description;
weatherTemperature.textContent =weather.temperature;
weatherBox.style.display="block";
load.style.display='none';
}