-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (114 loc) · 5.23 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const apiKey = 'ece06f1ffb434337b25123309241007';
const searchInput = document.getElementById('search-location');
const searchButton = document.getElementById('search-button');
const weatherInfo = document.getElementById('weather-info');
const temperature = document.getElementById('temperature');
const weatherCondition = document.getElementById('weather-condition');
const precipitation = document.getElementById('precipitation');
const cloudCover = document.getElementById('cloud-cover');
const weatherIcon = document.getElementById('weather-icon');
const backButton = document.getElementById('back-button');
const forecastContainer = document.getElementById('forecast-container');
const forecastSlider = document.getElementById('forecast-slider');
const prevHourButton = document.getElementById('prev-hour');
const nextHourButton = document.getElementById('next-hour');
let hourlyData = [];
let currentIndex = 0;
async function getWeatherData(location) {
const url = `https://api.weatherapi.com/v1/forecast.json?key=${apiKey}&q=${location}&hours=24`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
if (data.error) {
weatherInfo.textContent = `Error: ${data.error.message}`;
forecastContainer.style.display = 'none'; // Hide the forecast box on error
backButton.style.display = 'none'; // Hide the back button on error
} else {
const city = data.location.name;
const temperatureValue = data.current.temp_c;
const weatherConditionValue = data.current.condition.text;
const weatherIconUrl = data.current.condition.icon;
const precip = data.current.precip_mm;
const cloud = data.current.cloud;
// Get hourly forecast data
hourlyData = data.forecast.forecastday[0].hour;
currentIndex = 0; // Reset index
// Get current local time at the searched location
const localTime = data.location.localtime;
// Format the local time using toLocaleTimeString
const formattedTime = new Date(localTime).toLocaleTimeString('en-US', { hour12: true });
// Update weather info
weatherInfo.innerHTML = `
<h3>${formattedTime} - ${city} (${data.location.country})</h3>
<img id="weather-icon" src="${weatherIconUrl}" alt="${weatherConditionValue}">
<p id="temperature">Temperature: ${temperatureValue}°C</p>
<p id="weather-condition">Weather: ${weatherConditionValue}</p>
<p id="precipitation">Chance of Precipitation: ${precip}%</p>
<p id="cloud-cover">Cloud Cover: ${cloud}%</p>
`;
// Display hourly forecast
displayHourlyForecast();
forecastContainer.style.display = 'block'; // Show forecast box
backButton.style.display = 'block'; // Show back button
}
} catch (error) {
console.error(error);
weatherInfo.textContent = 'Error fetching weather data.';
forecastContainer.style.display = 'none'; // Hide the forecast box on error
backButton.style.display = 'none'; // Hide the back button on error
}
}
function displayHourlyForecast() {
forecastSlider.innerHTML = '';
const numberOfHours = hourlyData.length;
const itemsToShow = 6;
for (let i = currentIndex; i < Math.min(currentIndex + itemsToShow, numberOfHours); i++) {
const hourData = hourlyData[i];
const forecastElement = document.createElement('div');
forecastElement.classList.add('forecast-hour');
forecastElement.innerHTML = `
<p>${hourData.time.split(' ')[1]}</p>
<img src="${hourData.condition.icon}" alt="${hourData.condition.text}">
<p>${hourData.temp_c}°C</p>
<p>${hourData.condition.text}</p>
`;
forecastSlider.appendChild(forecastElement);
}
}
prevHourButton.addEventListener('click', () => {
if (currentIndex > 0) {
currentIndex -= 6;
displayHourlyForecast();
}
});
nextHourButton.addEventListener('click', () => {
if (currentIndex + 6 < hourlyData.length) {
currentIndex += 6;
displayHourlyForecast();
}
});
searchButton.addEventListener('click', () => {
const searchTerm = searchInput.value.trim();
if (!searchTerm) {
alert('Please enter a location to search.');
return;
}
getWeatherData(searchTerm);
});
backButton.addEventListener('click', () => {
searchInput.value = '';
weatherInfo.innerHTML = `
<h2>Get the weather details of any city, Right Now!</h2>
<img id="weather-icon" src="" alt="Weather Icon" style="visibility: hidden;">
<p id="temperature">Temperature: -°C</p>
<p id="weather-condition">Current Weather: </p>
<p id="precipitation">Chance of Precipitation: -%</p>
<p id="cloud-cover">Cloud Cover: -%</p>
`;
forecastSlider.innerHTML = ''; // Clear forecast data
forecastContainer.style.display = 'none'; // Hide the forecast box
backButton.style.display = 'none'; // Hide the back button
});