-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
37 lines (34 loc) · 903 Bytes
/
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
const url =
'https://api.openweathermap.org/data/2.5/weather';
const apiKey =
'dd1c70b13bcf47a7e8440b16048a24df';
$(document).ready(function () {
weatherFn('Pune');
});
async function weatherFn(cName) {
const temp =
`${url}?q=${cName}&appid=${apiKey}&units=metric`;
try {
const res = await fetch(temp);
const data = await res.json();
if (res.ok) {
weatherShowFn(data);
} else {
alert('City not found. Please try again.');
}
} catch (error) {
console.error('Error fetching weather data:', error);
}
}
function weatherShowFn(data) {
$('#city-name').text(data.name);
$('#date').text(moment().
format('MMMM Do YYYY, h:mm:ss a'));
$('#temperature').
html(`${data.main.temp}°C`);
$('#description').
text(data.weather[0].description);
$('#wind-speed').
html(`Wind Speed: ${data.wind.speed} m/s`);
$('#weather-info').fadeIn();
}