index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. let date = new Date(f[i].date);
  20. head.children[i+1].textContent = date.toLocaleDateString("en-US", {
  21. month: "short",
  22. day: "numeric",
  23. year: "2-digit"
  24. });
  25. tbody.children[0].children[i+1].textContent = `${f[i].day.mintemp_f} - ${f[i].day.maxtemp_f}`;
  26. tbody.children[1].children[i+1].textContent = `${f[i].day.avghumidity}%`;
  27. tbody.children[2].children[i+1].textContent = `${f[i].day.daily_chance_of_rain}%`;
  28. tbody.children[3].children[i+1].textContent = f[i].day.totalprecip_in;
  29. tbody.children[4].children[i+1].textContent = f[i].day.maxwind_mph;
  30. tbody.children[5].children[i+1].textContent = f[i].astro.sunrise;
  31. tbody.children[6].children[i+1].textContent = f[i].astro.sunset;
  32. }
  33. }
  34. const getWeather = ()=>{
  35. fetch("https://api.weatherapi.com/v1/forecast.json?key=519f51763fa8477b864184849251502&q=29576&days=3&aqi=no", {
  36. method: "get"
  37. })
  38. .then(r=>r.json())
  39. .then((response)=>{
  40. console.log(response);
  41. const tempDisplay = `Temperature: ${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.querySelector("#currentWeather img");
  46. currentWeatherImage.src = `https:${response.current.condition.icon}`;
  47. currentWeatherImage.alt = response.current.condition.text;
  48. const currentWeatherText = document.querySelector("#currentWeather .conditionText");
  49. currentWeatherText.textContent = response.current.condition.text;
  50. const currentWind = `Wind: ${response.current.wind_mph}mph ${response.current.wind_dir}`;
  51. document.getElementById("wind").textContent = currentWind;
  52. const humidity = `Humidity: ${response.current.humidity}%`;
  53. document.getElementById("humidity").textContent = humidity;
  54. renderForecast(response.forecast.forecastday);
  55. })
  56. .catch((err)=>{
  57. console.log(err);
  58. });
  59. }
  60. const getRadarImage = () =>{
  61. const img = document.getElementById("radar");
  62. const base = img.src.split("?")[0];
  63. img.src = base + "?t=" + Date.now();
  64. }
  65. getWeather();
  66. setInterval(getWeather, 60000);
  67. setDateTime();
  68. setInterval(setDateTime, 10000);
  69. setInterval(getRadarImage, 10 * 1000);