-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
84 lines (63 loc) · 2.81 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
const input = document.getElementById('input');
const button = document.getElementById('btn');
const cityNames = document.getElementById("city-name");
const cityTime = document.getElementById('city-time');
const cityTemp = document.getElementById('city-temp');
const cityTimeZone = document.getElementById('city-timezone');
// Button loading effect
function setButtonLoading(isLoading) {
if (isLoading) {
button.disabled = true;
button.classList.add('button-loading');
button.innerHTML = 'Fetching...';
} else {
button.disabled = false;
button.classList.remove('button-loading');
button.innerHTML = 'Search';
}
}
async function getData(cityName){
const promise = await fetch(
`http://api.weatherapi.com/v1/current.json?key=2b340cdd09b042b1b9a94825241909&q=${cityName}&aqi=yes`
);
return await promise.json();
}
button.addEventListener("click", async () => {
const value = input.value;
// Start loading spinner
setButtonLoading(true);
try {
const result = await getData(value);
console.log(result); // For debugging
cityNames.innerHTML = `<span>City:</span> ${result.location.name}, ${result.location.region} - ${result.location.country}`;
cityTime.innerHTML = `<span>Local Time:</span> ${result.location.localtime}`;
cityTemp.innerHTML = `<span>Temperature (°C):</span> ${result.current.temp_c}`;
cityTimeZone.innerHTML = `<span>Time Zone:</span> ${result.location.tz_id}`;
} catch (error) {
console.error("Error fetching weather data", error);
} finally {
// Stop loading spinner
setButtonLoading(false);
}
});
// const input = document.getElementById('input');
// const button = document.getElementById('btn');
// const cityNames = document.getElementById("city-name");
// const cityTime = document.getElementById('city-time');
// const cityTemp = document.getElementById('city-temp');
// const cityTimeZone = document.getElementById('city-timezone');
// async function getData(cityName){
// const promise = await fetch(
// `http://api.weatherapi.com/v1/current.json?key=2b340cdd09b042b1b9a94825241909&q=${cityName}&aqi=yes`
// );
// return await promise.json();
// }
// button.addEventListener("click", async () => {
// const value = input.value;
// const result = await getData(value);
// console.log(result); // For debugging
// cityNames.innerHTML = `<span>City:</span> ${result.location.name}, ${result.location.region} - ${result.location.country}`;
// cityTime.innerHTML = `<span>Local Time:</span> ${result.location.localtime}`;
// cityTemp.innerHTML = `<span>Temperature (°C):</span> ${result.current.temp_c}`;
// cityTimeZone.innerHTML = `<span>Time Zone:</span> ${result.location.tz_id}`;
// });