index.js 3.2 KB

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