index.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const setDateTime = ()=>{
  2. const dateTime = new Date();
  3. document.getElementById("date").textContent = dateTime.toLocaleDateString("en-US", {
  4. weekday: "long",
  5. year: "numeric",
  6. month: "long",
  7. day: "numeric"
  8. });
  9. document.getElementById("time").textContent = dateTime.toLocaleTimeString("en-US", {
  10. hour: "numeric",
  11. minute: "2-digit",
  12. hour12: true
  13. });
  14. }
  15. const renderForecast = (f)=>{
  16. const head = document.getElementById("forecastHead").children[0];
  17. const tbody = document.getElementById("forecastBody");
  18. for(let i = 0; i < f.length; i++){
  19. const [y, m, d] = f[i].date.split("-").map(Number);
  20. const date = new Date(y, m-1, d);
  21. head.children[i+1].textContent = date.toLocaleDateString("en-US", {
  22. month: "short",
  23. day: "numeric",
  24. year: "2-digit"
  25. });
  26. tbody.children[0].children[i+1].textContent = `${f[i].day.mintemp_f} - ${f[i].day.maxtemp_f}`;
  27. tbody.children[1].children[i+1].textContent = `${f[i].day.avghumidity}%`;
  28. tbody.children[2].children[i+1].textContent = `${f[i].day.daily_chance_of_rain}%`;
  29. tbody.children[3].children[i+1].textContent = f[i].day.totalprecip_in;
  30. tbody.children[4].children[i+1].textContent = f[i].day.maxwind_mph;
  31. tbody.children[5].children[i+1].textContent = f[i].astro.sunrise;
  32. tbody.children[6].children[i+1].textContent = f[i].astro.sunset;
  33. }
  34. }
  35. const getWeather = ()=>{
  36. fetch("https://api.weatherapi.com/v1/forecast.json?key=519f51763fa8477b864184849251502&q=29576&days=3&aqi=no", {
  37. method: "get"
  38. })
  39. .then(r=>r.json())
  40. .then((response)=>{
  41. console.log(response);
  42. const tempDisplay = `Temperature: ${response.current.temp_f}\u00B0F`;
  43. document.getElementById("temp").textContent = tempDisplay;
  44. const feelsLike = `Feels Like: ${response.current.feelslike_f}\u00B0F`;
  45. document.getElementById("feels").textContent = feelsLike;
  46. const currentWeatherImage = document.querySelector("#currentWeather img");
  47. currentWeatherImage.src = `https:${response.current.condition.icon}`;
  48. currentWeatherImage.alt = response.current.condition.text;
  49. const currentWeatherText = document.querySelector("#currentWeather .conditionText");
  50. currentWeatherText.textContent = response.current.condition.text;
  51. const currentWind = `Wind: ${response.current.wind_mph}mph ${response.current.wind_dir}`;
  52. document.getElementById("wind").textContent = currentWind;
  53. const humidity = `Humidity: ${response.current.humidity}%`;
  54. document.getElementById("humidity").textContent = humidity;
  55. renderForecast(response.forecast.forecastday);
  56. })
  57. .catch((err)=>{
  58. console.log(err);
  59. });
  60. }
  61. const getRadarImage = () =>{
  62. const img = document.getElementById("radar");
  63. const base = img.src.split("?")[0];
  64. img.src = base + "?t=" + Date.now();
  65. }
  66. getWeather();
  67. setInterval(getWeather, 60000);
  68. setDateTime();
  69. setInterval(setDateTime, 10000);
  70. setInterval(getRadarImage, 10 * 1000);