index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 = `${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.getElementById("conditionLogo");
  47. currentWeatherImage.src = `https:${response.current.condition.icon}`;
  48. currentWeatherImage.alt = response.current.condition.text;
  49. const currentWind = `Winds ${response.current.wind_mph}mph ${response.current.wind_dir}`;
  50. const currentElem = document.querySelector("#wind p");
  51. const newElem = document.createElement("p");
  52. newElem.textContent = currentWind;
  53. currentElem.replaceWith(newElem);
  54. document.querySelector("#wind svg").style.transform = `rotate(${response.current.wind_degree}deg)`;
  55. const humidity = `${response.current.humidity}% humidity`;
  56. document.getElementById("humidity").textContent = humidity;
  57. renderForecast(response.forecast.forecastday);
  58. })
  59. .catch((err)=>{
  60. console.log(err);
  61. });
  62. }
  63. const getRadarImage = () =>{
  64. const img = document.getElementById("radar");
  65. const base = img.src.split("?")[0];
  66. img.src = base + "?t=" + Date.now();
  67. }
  68. getWeather();
  69. setInterval(getWeather, 60000);
  70. setDateTime();
  71. setInterval(setDateTime, 10000);
  72. setInterval(getRadarImage, 10 * 1000);