index.js 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import getWeatherInfo from "./weatherCodes.js";
  2. const setDateTime = ()=>{
  3. const dateTime = new Date();
  4. document.getElementById("date").textContent = dateTime.toLocaleDateString("en-US", {
  5. weekday: "long",
  6. year: "numeric",
  7. month: "long",
  8. day: "numeric"
  9. });
  10. document.getElementById("time").textContent = dateTime.toLocaleTimeString("en-US", {
  11. hour: "numeric",
  12. minute: "2-digit",
  13. hour12: true
  14. });
  15. }
  16. const renderForecast = (f)=>{
  17. const head = document.getElementById("forecastHead").children[0];
  18. const tbody = document.getElementById("forecastBody");
  19. for(let i = 0; i < 3; i++){
  20. const sunrise = new Date(f.sunrise[i]);
  21. const sunset = new Date(f.sunset[i]);
  22. const timeOptions = {
  23. hour: "2-digit",
  24. minute: "2-digit",
  25. hour12: true
  26. };
  27. head.children[i+1].textContent = new Date(f.time[i]).toLocaleDateString("en-US", {
  28. month: "short",
  29. day: "numeric",
  30. year: "2-digit"
  31. });
  32. tbody.children[0].children[i+1].textContent = `${f.temperature_2m_min[i]} - ${f.temperature_2m_max[i]}`;
  33. tbody.children[1].children[i+1].textContent = f.dew_point_2m_mean[i];
  34. tbody.children[2].children[i+1].textContent = `${f.precipitation_probability_max[i]}%`;
  35. tbody.children[3].children[i+1].textContent = f.precipitation_sum[i].toFixed(2);
  36. tbody.children[4].children[i+1].textContent = f.wind_speed_10m_max[i];
  37. tbody.children[5].children[i+1].textContent = sunrise.toLocaleTimeString("en-US", timeOptions);
  38. tbody.children[6].children[i+1].textContent = sunset.toLocaleTimeString("en-US", timeOptions);
  39. }
  40. }
  41. const getWeather = ()=>{
  42. fetch("https://api.open-meteo.com/v1/forecast?latitude=33.54&longitude=-79.05&daily=temperature_2m_max,temperature_2m_min,sunrise,sunset,rain_sum,precipitation_probability_max,wind_speed_10m_max,wind_gusts_10m_max,precipitation_sum,wind_direction_10m_dominant,weather_code,dew_point_2m_mean&current=temperature_2m,apparent_temperature,weather_code,wind_speed_10m,wind_direction_10m,is_day,dew_point_2m&timezone=America%2FNew_York&wind_speed_unit=mph&temperature_unit=fahrenheit&precipitation_unit=inch", {method: "GET"})
  43. .then(r=>r.json())
  44. .then((response)=>{
  45. const tempDisplay = `${response.current.temperature_2m}\u00B0F`;
  46. document.getElementById("temp").textContent= tempDisplay;
  47. const feelsLike = `Feels Like ${response.current.apparent_temperature}\u00B0F`;
  48. document.getElementById("feels").textContent = feelsLike;
  49. const weatherInfo = getWeatherInfo(response.current.weather_code, response.current.is_day);
  50. document.getElementById("weatherDescription").textContent = weatherInfo.desc;
  51. document.getElementById("currentWeatherLogo").textContent = weatherInfo.icon;
  52. const currentWind = `Wind ${response.current.wind_speed_10m}mph`;
  53. const currentElem = document.querySelector("#wind p");
  54. const newElem = document.createElement("p");
  55. newElem.textContent = currentWind;
  56. currentElem.replaceWith(newElem);
  57. document.querySelector("#wind svg").style.transform = `rotate(${response.current.wind_direction_10m}deg)`;
  58. const dewPoint = `Dew Point: ${response.current.dew_point_2m}\u00B0F`;
  59. document.getElementById("dewPoint").textContent = dewPoint;
  60. renderForecast(response.daily);
  61. })
  62. .catch((err)=>{
  63. console.error(err);
  64. });
  65. }
  66. const getRadarImage = () =>{
  67. const img = document.getElementById("radar");
  68. const base = img.src.split("?")[0];
  69. img.src = base + "?t=" + Date.now();
  70. }
  71. getWeather();
  72. setInterval(getWeather, 60000);
  73. setDateTime();
  74. setInterval(setDateTime, 10000);
  75. setInterval(getRadarImage, 10 * 1000);