index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 getWeather = ()=>{
  16. fetch("https://api.weatherapi.com/v1/forecast.json?key=519f51763fa8477b864184849251502&q=29576&days=3&aqi=no", {
  17. method: "get"
  18. })
  19. .then(r=>r.json())
  20. .then((response)=>{
  21. console.log(response);
  22. const tempDisplay = `Temperature: ${response.current.temp_f}\u00B0F`;
  23. document.getElementById("temp").textContent = tempDisplay;
  24. const feelsLike = `Feels Like: ${response.current.feelslike_f}\u00B0F`;
  25. document.getElementById("feels").textContent = feelsLike;
  26. const currentWeatherImage = document.querySelector("#currentWeather img");
  27. currentWeatherImage.src = `https:${response.current.condition.icon}`;
  28. currentWeatherImage.alt = response.current.condition.text;
  29. const currentWeatherText = document.querySelector("#currentWeather .conditionText");
  30. currentWeatherText.textContent = response.current.condition.text;
  31. const currentWind = `Wind: ${response.current.wind_mph}mph ${response.current.wind_dir}`;
  32. document.getElementById("wind").textContent = currentWind;
  33. const humidity = `Humidity: ${response.current.humidity}%`;
  34. document.getElementById("humidity").textContent = humidity;
  35. })
  36. .catch((err)=>{
  37. console.log(err);
  38. });
  39. }
  40. const getRadarImage = () =>{
  41. const img = document.getElementById("radar");
  42. const base = img.src.split("?")[0];
  43. img.src = base + "?t=" + Date.now();
  44. }
  45. getWeather();
  46. setInterval(getWeather, 60000);
  47. setDateTime();
  48. setInterval(setDateTime, 10000);
  49. setInterval(getRadarImage, 10 * 1000);