index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. document.getElementById("one").textContent = window.innerWidth;
  2. document.getElementById("two").textContent = window.innerHeight;
  3. document.getElementById("three").textContent = window.devicePixelRatio;
  4. document.getElementById("four").textContent = navigator.userAgent;
  5. const setDateTime = ()=>{
  6. const dateTime = new Date();
  7. document.getElementById("date").textContent = dateTime.toLocaleDateString("en-US", {
  8. weekday: "long",
  9. year: "numeric",
  10. month: "long",
  11. day: "numeric"
  12. });
  13. document.getElementById("time").textContent = dateTime.toLocaleTimeString("en-US", {
  14. hour: "numeric",
  15. minute: "2-digit",
  16. hour12: true
  17. });
  18. }
  19. const renderForecast = (f)=>{
  20. const head = document.getElementById("forecastHead").children[0];
  21. const tbody = document.getElementById("forecastBody");
  22. for(let i = 0; i < f.length; i++){
  23. const [y, m, d] = f[i].date.split("-").map(Number);
  24. const date = new Date(y, m-1, d);
  25. head.children[i+1].textContent = date.toLocaleDateString("en-US", {
  26. month: "short",
  27. day: "numeric",
  28. year: "2-digit"
  29. });
  30. tbody.children[0].children[i+1].textContent = `${f[i].day.mintemp_f} - ${f[i].day.maxtemp_f}`;
  31. tbody.children[1].children[i+1].textContent = `${f[i].day.avghumidity}%`;
  32. tbody.children[2].children[i+1].textContent = `${f[i].day.daily_chance_of_rain}%`;
  33. tbody.children[3].children[i+1].textContent = f[i].day.totalprecip_in;
  34. tbody.children[4].children[i+1].textContent = f[i].day.maxwind_mph;
  35. tbody.children[5].children[i+1].textContent = f[i].astro.sunrise;
  36. tbody.children[6].children[i+1].textContent = f[i].astro.sunset;
  37. }
  38. }
  39. const getWeather = ()=>{
  40. fetch("https://api.weatherapi.com/v1/forecast.json?key=519f51763fa8477b864184849251502&q=29576&days=3&aqi=no", {
  41. method: "get"
  42. })
  43. .then(r=>r.json())
  44. .then((response)=>{
  45. console.log(response);
  46. const tempDisplay = `Temperature: ${response.current.temp_f}\u00B0F`;
  47. document.getElementById("temp").textContent = tempDisplay;
  48. const feelsLike = `Feels Like: ${response.current.feelslike_f}\u00B0F`;
  49. document.getElementById("feels").textContent = feelsLike;
  50. const currentWeatherImage = document.querySelector("#currentWeather img");
  51. currentWeatherImage.src = `https:${response.current.condition.icon}`;
  52. currentWeatherImage.alt = response.current.condition.text;
  53. const currentWeatherText = document.querySelector("#currentWeather .conditionText");
  54. currentWeatherText.textContent = response.current.condition.text;
  55. const currentWind = `Wind: ${response.current.wind_mph}mph ${response.current.wind_dir}`;
  56. document.getElementById("wind").textContent = currentWind;
  57. const humidity = `Humidity: ${response.current.humidity}%`;
  58. document.getElementById("humidity").textContent = humidity;
  59. renderForecast(response.forecast.forecastday);
  60. })
  61. .catch((err)=>{
  62. console.log(err);
  63. });
  64. }
  65. const getRadarImage = () =>{
  66. const img = document.getElementById("radar");
  67. const base = img.src.split("?")[0];
  68. img.src = base + "?t=" + Date.now();
  69. }
  70. getWeather();
  71. setInterval(getWeather, 60000);
  72. setDateTime();
  73. setInterval(setDateTime, 10000);
  74. setInterval(getRadarImage, 10 * 1000);