Zeigen Sie Bestandszähler an mehreren Standorten auf derselben Seite an
P粉038161873
P粉038161873 2024-04-02 14:25:38
0
2
2536

Dieses Skript eignet sich hervorragend als Lagerbestands-Countdown, aber ich kann es nicht an mehr als einer Stelle auf derselben Seite anzeigen.

Zum Beispiel gibt es 4 oder 5 Produkte/Pläne auf derselben Seite, jedes Produkt zeigt eine andere Lagerbestandsmenge an und die Menge ändert sich zu unterschiedlichen Zeiten. Wird nur auf einem Produkt angezeigt.

<span class="qty" id="qty"></span>

<script>
    const setQty = (qty) => {
        qtySpan.innerHTML = qty;

        if (qty == 0) return;

        let parts = Math.floor((Math.random() * 3) + 1);
        if (parts > qty) parts = qty;

        const msec =  Math.floor(((Math.random() * 15) + 15) * 1000);
        qty -= parts;

        // Save the updated quantity in localStorage
        localStorage.setItem('saved_countdown', qty);

        setTimeout(() => setQty(qty), msec);
    }

    // Get the saved countdown value from localStorage, or use default value of 57 if not found
    const defaultQty = localStorage.getItem('saved_countdown') ?? 57;
    
    const qtySpan = document.getElementById('qty');

    // Set the initial value of the quantity
    setQty(defaultQty);
</script>

Ich habe das Skript 4x kopiert und die ID „qty“ in qty1, qty2, qty3 und qty4 geändert, aber es funktioniert nicht, es wird nur in einem Produkt angezeigt... :/

Kann mir hier jemand helfen? Danke!

P粉038161873
P粉038161873

Antworte allen(2)
P粉536532781

我更改了功能,以便您可以有多个倒计时。

const setQty = (qtySpan, qty) => {
qtySpan.innerHTML = qty;

if (qty == 0) return;

let parts = Math.floor((Math.random() * 3) + 1);
if (parts > qty) parts = qty;

const msec = Math.floor(((Math.random() * 15) + 15) * 1000);
qty -= parts;

// Save the updated quantity in localStorage
localStorage.setItem('saved_countdown', qty);

setTimeout(() => setQty(qty), msec);
}

const defaultQty = localStorage.getItem('saved_countdown') ?? 57;

let listOfQty = document.querySelectorAll('.qty');
listOfQty.forEach((qty) =>{
    setQty(qty, defaultQty);

})

您只需发送每个倒计时的具体数字作为第二个参数。

P粉761718546

我已将您的脚本修改为自定义元素。这意味着您现在可以创建一个名为 <stock-counter> 的元素。

该元素有 2 个属性,quantitystorage-key

  1. quantity 是开始计数的金额。
  2. storage-key 是本地存储密钥的名称,用于存储特定计数器的最后数量。如果设置了存储键并且找到了存储值,则该值将取代 quanitity 值,除非存储值是 0

所以元素看起来像这样:

40

您可以在页面上放置任意数量的这些元素,并修改每个元素的 quantitystorage-key

customElements.define('stock-counter', class extends HTMLElement {
  get quantity() {
    // Check if value has been stored.
    if (this.storageKey !== null) {
      const value = Number(localStorage.getItem(this.storageKey));

      // Use that value if it is a valid number and not 0.
      if (!Number.isNaN(value) && value !== 0) {
        return value;
      }
    }

    // Otherwise get the value from the quantity attribute.
    const value = Number(this.getAttribute('quantity'));

    if (Number.isNaN(value)) {
      return 0;
    }

    return value;
  }

  set quantity(value) {
    if (!isNaN(value)) {
    
      // Store the new value if it's possible.
      if (this.storageKey !== null) {
        localStorage.setItem(this.storageKey, value);
      }

      // Set the new attribute value.
      this.setAttribute('quantity', value);
    }
  }

  get storageKey() {
    return this.getAttribute('storage-key');
  }

  connectedCallback() {
    this.count();
  }

  count = () => {
    const qty = this.quantity;
    this.textContent = qty;

    if (qty === 0) {
      return;
    }

    let parts = Math.floor((Math.random() * 3) + 1);

    if (parts > qty) {
      parts = qty;
    }

    this.quantity -= parts;

    const msec = Math.floor(((Math.random() * 15) + 15) * 1000);
    setTimeout(this.count, msec);
  };
});
40
50
80
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!