Themabewertung:
  • 0 Bewertung(en) - 0 im Durchschnitt
  • 1
  • 2
  • 3
  • 4
  • 5
Alert Fenster
#1
Hallo ihr Lieben, ich habe mal eine frage und zwar habe ich eine AQI Partikel Anzeige und will nun wenn es ab 100 ist das es dann eine Meldung gibt wie kann ich das realisiern?
Am besten wäre es mir das es als modales Popup anzeigt.


Code:
const currentAqi = document.getElementById("current-aqi");
const indicator = document.getElementById("indicator");
// API key
const key = "a442b81f-81f2-42a6-a08a-ac4796a8d4cb";
// Geolocation coordinates
let latitude;
let longitude;

// Function to update geolocation
function updateGeolocation(position) {
  latitude = position.coords.latitude;
  longitude = position.coords.longitude;
}

// Check if geolocation is available
function checkGeolocation() {
  if ("geolocation" in navigator) {
    // Get the current geolocation
    navigator.geolocation.getCurrentPosition(updateGeolocation);
  } else {
    console.log("Geolocation is not available.");
  }
}

// Fetch data from the API
function fetchData() {
  const apiUrl = `https://api.airvisual.com/v2/nearest_city?lat=${latitude}&lon=${longitude}&key=${key}`;
  // Fetch data from the API
  fetch(apiUrl).then(handleResponse).then(processData).catch(handleError);
}

// Handle response from API
function handleResponse(response) {
  if (!response.ok) {
    throw new Error(`HTTP error! Status: ${response.status}`);
  }
  return response.json(); // Parse the response as JSON
}

// Process data and update values
function processData(data) {
  const calculateValue = (aqi) => (aqi / 500) * 350 + 5;
  let aqi = 0;
  let result = calculateValue(aqi);
  let intervalId;

  // Update AQI values and rotation
  function updateValues() {
    console.log(`AQI: ${aqi}, DEG: ${result}`);
     

    // Update the AQI display using SVG code
    currentAqi.innerHTML = `${aqi}<br>`;

    // Update the rotation of the indicator
    indicator.style.rotate = `${result}deg`;

    // Increment AQI and calculate corresponding rotation
    aqi += 1;
    result = calculateValue(aqi);

    // Check if the target AQI has been reached
    if (aqi > data.data.current.pollution.aqius) {
      clearInterval(intervalId);
    }
  }

  // Set interval for updating values
  intervalId = setInterval(updateValues, 100);
}

// Handle errors during data fetching
function handleError(error) {
  console.error("Fetch error:", error);
}

// Initiate data retrieval and updates
function initializeApp() {
  checkGeolocation();
  fetchData();
}

// Call the initializeApp function
initializeApp();
 
Code:
<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CodePen - AQI Particle Matter 2.5  (Current Location)</title>
  <meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="./particle/style.css">
  <meta http-equiv="refresh" content="1800">
</head>
<body>
<!-- partial:index.partial.html -->
<div id="bg">
  <div id="meter">
    <h1 id="current-aqi">---</h1>
    <p>
      <!--PM (Particle Matter) SVG-->
      <svg height="15px" viewBox="0 0 315 359" fill="none" xmlns="http://www.w3.org/2000/svg">
        <g id="PM">
          <circle id="Ellipse 1" cx="158" cy="27" r="27" />
          <circle id="Ellipse 2" cx="222" cy="68" r="27" />
          <circle id="Ellipse 3" cx="288" cy="109" r="27" />
          <circle id="Ellipse 4" cx="92" cy="67" r="27" />
          <circle id="Ellipse 5" cx="27" cy="109" r="27" />
          <circle id="Ellipse 6" cx="92.5" cy="141.5" r="26.5" />
          <circle id="Ellipse 7" cx="157" cy="101" r="27" />
          <circle id="Ellipse 8" cx="157.5" cy="174.5" r="26.5" />
          <circle id="Ellipse 9" cx="223" cy="142" r="26" />
          <circle id="Ellipse 10" cx="27" cy="178" r="21" />
          <circle id="Ellipse 11" cx="92" cy="210" r="21" />
          <circle id="Ellipse 12" cx="157" cy="243" r="21" />
          <circle id="Ellipse 13" cx="223" cy="210" r="21" />
          <circle id="Ellipse 14" cx="288" cy="178" r="21" />
          <circle id="Ellipse 15" cx="288" cy="236" r="16" />
          <circle id="Ellipse 16" cx="223" cy="268" r="16" />
          <circle id="Ellipse 17" cx="157" cy="301" r="16" />
          <circle id="Ellipse 18" cx="92" cy="268" r="16" />
          <circle id="Ellipse 19" cx="27" cy="236" r="16" />
          <circle id="Ellipse 20" cx="27.5" cy="282.5" r="10.5" />
          <circle id="Ellipse 21" cx="92.5" cy="315.5" r="10.5" />
          <circle id="Ellipse 22" cx="157.5" cy="348.5" r="10.5" />
          <circle id="Ellipse 23" cx="222.5" cy="315.5" r="10.5" />
          <circle id="Ellipse 24" cx="288.5" cy="282.5" r="10.5" />
        </g>
      </svg> AQI</p>
    <div id="indicator"></div>
  </div>
</div>
<!-- partial -->
  <script  src="./particle/script.js"></script>

</body>
</html>
Ich danke euch jetzt schon mal für eure hilfe.
Zitieren
#2
Hallo und willkommen im Forum!

Die Stelle wo Du das Popup einfügen musst, lässt sich leicht finden:
Code:
        // Check if the target AQI has been reached
        if (aqi > data.data.current.pollution.aqius) {
            clearInterval(intervalId);
            alert('Target AQI is reached');
        }
Wie Du ein Popup bzw. Modal erzeugst, kannst Du hier nachlesen:
https://wiki.selfhtml.org/wiki/Infobox/m...logfenster
Versuche, ob Du damit zum Ziel kommst und melde dich wieder wenn nicht.
Glaube denen, die die Wahrheit suchen, und zweifle an denen, die sie gefunden haben.
(Andrι Gide (1869-1951), frz. Schriftst., 1947 Nobelpreis)
Zitieren
#3
PS: Ich bin mir jetzt nicht mehr sicher ob ich dich richtig verstanden habe: Die Abfrage dort fragt ja einen variablen Wert ab, Du schreibst jedoch von 100. War das nur ein Beispiel oder möchtest Du diesen festen Wert verwenden?
Glaube denen, die die Wahrheit suchen, und zweifle an denen, die sie gefunden haben.
(Andrι Gide (1869-1951), frz. Schriftst., 1947 Nobelpreis)
Zitieren
#4
Also sobald der Particel wert auf 100 ist soll eine warnung aufpopen, nun weis ich nicht genau wie ich das anstellen soll.

(04.05.2024, 16:39)Sempervivum schrieb: Hallo und willkommen im Forum!

Die Stelle wo Du das Popup einfügen musst, lässt sich leicht finden:
Code:
        // Check if the target AQI has been reached
        if (aqi > data.data.current.pollution.aqius) {
            clearInterval(intervalId);
            alert('Target AQI is reached');
        }
Wie Du ein Popup bzw. Modal erzeugst, kannst Du hier nachlesen:
https://wiki.selfhtml.org/wiki/Infobox/m...logfenster
Versuche, ob Du damit zum Ziel kommst und melde dich wieder wenn nicht.


Meinst du das so??? aber es soll automatisch aufpoppen wenn der Partikel wert 100 erreicht hat.
Code:
document.getElementById("show-dialog").addEventListener("click", () => {
    document.getElementById("dialog").showModal();
});
Zitieren
#5
Also ich müsste ja nur die id auslesen und wenn da dann 100 steht kommt dann ein alarm popup 
Code:
<h1 id="current-aqi">---</h1>

document.getElementById("current-aqi");

Wäre das möglich ??
Zitieren
#6
Hallo, wegen zahlreicher anderer Aktivitäten, u. a. Polarlichter, habe ich in der Zwischenzeit keine Zeit gefunden, zu antworten.
Ist diese Sache überhaupt noch aktuell?
Glaube denen, die die Wahrheit suchen, und zweifle an denen, die sie gefunden haben.
(Andrι Gide (1869-1951), frz. Schriftst., 1947 Nobelpreis)
Zitieren


Gehe zu:


Benutzer, die gerade dieses Thema anschauen:
1 Gast/Gäste