-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
99 lines (65 loc) · 3.31 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const apiKey = "9470f162a34d93086760b16d1426a9ed";
const apiUrl = "https://api.openweathermap.org/data/2.5/weather?units=metric&q="
const searchBox = document.querySelector(".search input");
const searchButton = document.querySelector(".search button");
async function getWeather(city) {
const responce = await fetch(apiUrl + city + `&appid=${apiKey}`);
if (responce.status == 404) {
// alert("City not found!");
let img = document.querySelector("#icon"); img.style.width = "30rem";
document.querySelector(".detail").style.display = "none";
document.querySelector(".temp2").style.display = "none";
img.src = 'images/404.png';
let temp = document.querySelector(".temp").innerHTML = "Error! 404";
let name = document.querySelector(".name").innerHTML = "City Not Found"
document.getElementById("temp").style.fontSize = "3rem";
return;
}
else {
var data = await responce.json();
// console.log(data);
document.querySelector(".temp").innerHTML = Math.round(data.main.temp) + "°c";
document.querySelector(".name").innerHTML = data.name + ", " + data.sys.country;
document.querySelector(".mintemp").innerHTML = Math.round(data.main.temp_min) + "°c";
document.querySelector(".maxtemp").innerHTML = Math.round(data.main.temp_max) + "°c";
document.querySelector(".humidity").innerHTML = data.main.humidity + "%";
document.querySelector(".wind").innerHTML = Math.round(data.wind.speed) + " km/h";
let img = document.querySelector("#icon");
// img.src = `http://openweathermap.org/img/wn/${data.weather[0].icon}@4x.png`;
switch (data.weather[0].main) {
case 'Clear':
img.src = 'images/clear.png';
break;
case 'Rain':
img.src = 'images/rain.png';
break;
case 'Snow':
img.src = 'images/snow.png';
break;
case 'Clouds':
img.src = 'images/cloud.png';
break;
case 'Mist':
img.src = 'images/mist.png';
break;
case 'Haze':
img.src = 'images/mist.png';
break;
default:
img.src = 'images/cloud.png';
break;
}
document.querySelector(".detail").style.display = "flex";
document.querySelector(".temp2").style.display = "flex";
document.getElementById("temp").style.fontSize = "5.2rem";
img.style.width = "16rem";
}
}
searchButton.addEventListener("click", function () {
getWeather(searchBox.value);
})
// if (searchBox.value != " ") {
setInterval(() => {
getWeather(searchBox.value);
}, 600000);
// }