
在网页中,我们可能需要在同一页面上展示多个产品或计划的库存倒计时,并且每个倒计时都应独立运行,拥有各自的初始数量和状态持久化能力。原始的javascript脚本通常通过document.getelementbyid('qty')来获取显示库存的元素,并通过localstorage.setitem('saved_countdown', qty)来保存库存值。这种方法存在两个核心问题:
为了解决这些问题,我们需要一种更模块化、可复用且能够封装自身状态的解决方案。Web Components中的自定义元素(Custom Elements)正是为此而生。
通过将库存计数器封装为一个自定义元素<stock-counter>,我们可以实现完全独立的多个计数器实例。每个实例将拥有自己的属性来管理初始数量和持久化存储键,从而避免全局冲突。
我们设计一个名为<stock-counter>的自定义元素,它将具有以下两个关键属性:
以下是<stock-counter>自定义元素的完整实现代码:
customElements.define('stock-counter', class extends HTMLElement {
// 获取当前库存数量的getter
get quantity() {
// 检查是否设置了storageKey,并尝试从localStorage获取存储值
if (this.storageKey !== null) {
const value = Number(localStorage.getItem(this.storageKey));
// 如果存储值是有效数字且不为0,则使用存储值
if (!Number.isNaN(value) && value !== 0) {
return value;
}
}
// 否则,从元素的quantity属性获取初始值
const value = Number(this.getAttribute('quantity'));
// 如果属性值不是有效数字,则返回0
if (Number.isNaN(value)) {
return 0;
}
return value;
}
// 设置库存数量的setter
set quantity(value) {
if (!isNaN(value)) {
// 如果设置了storageKey,则将新值存储到localStorage
if (this.storageKey !== null) {
localStorage.setItem(this.storageKey, value);
}
// 更新元素的quantity属性
this.setAttribute('quantity', value);
// 更新元素的文本内容以显示最新数量
this.textContent = value; // 确保在设置时也更新显示
}
}
// 获取storage-key属性的getter
get storageKey() {
return this.getAttribute('storage-key');
}
// 当元素被添加到文档DOM时调用
connectedCallback() {
// 初始化显示数量
this.textContent = this.quantity;
// 启动计数器
this.count();
}
// 核心计数逻辑
count = () => {
let qty = this.quantity; // 获取当前数量
if (qty === 0) {
this.textContent = 0; // 确保显示为0
return; // 数量为0时停止计数
}
// 随机减少1到3个单位
let parts = Math.floor((Math.random() * 3) + 1);
// 确保减少的数量不超过当前库存
if (parts > qty) {
parts = qty;
}
// 更新库存数量(通过setter会自动更新localStorage和属性)
this.quantity -= parts;
// 随机延迟15到30秒后再次计数
const msec = Math.floor(((Math.random() * 15) + 15) * 1000);
setTimeout(this.count, msec);
};
});一旦自定义元素被定义,你就可以像使用任何标准HTML标签一样在页面中使用它。
PHP经典实例(第2版)能够为您节省宝贵的Web开发时间。有了这些针对真实问题的解决方案放在手边,大多数编程难题都会迎刃而解。《PHP经典实例(第2版)》将PHP的特性与经典实例丛书的独特形式组合到一起,足以帮您成功地构建跨浏览器的Web应用程序。在这个修订版中,您可以更加方便地找到各种编程问题的解决方案,《PHP经典实例(第2版)》中内容涵盖了:表单处理;Session管理;数据库交互;使用We
453
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>多处独立库存计数器示例</title>
</head>
<body>
<h1>产品库存状态</h1>
<div>
<h2>产品 A</h2>
<p>剩余库存:<stock-counter quantity="40" storage-key="countdown-one">40</stock-counter> 件</p>
</div>
<div>
<h2>产品 B (无持久化)</h2>
<p>剩余库存:<stock-counter quantity="50">50</stock-counter> 件</p>
</div>
<div>
<h2>产品 C</h2>
<p>剩余库存:<stock-counter quantity="80" storage-key="countdown-three">80</stock-counter> 件</p>
</div>
<script>
// 将自定义元素的定义代码放在这里,或者引入一个外部JS文件
customElements.define('stock-counter', class extends HTMLElement {
get quantity() {
if (this.storageKey !== null) {
const value = Number(localStorage.getItem(this.storageKey));
if (!Number.isNaN(value) && value !== 0) {
return value;
}
}
const value = Number(this.getAttribute('quantity'));
if (Number.isNaN(value)) {
return 0;
}
return value;
}
set quantity(value) {
if (!isNaN(value)) {
if (this.storageKey !== null) {
localStorage.setItem(this.storageKey, value);
}
this.setAttribute('quantity', value);
this.textContent = value; // 确保在设置时也更新显示
}
}
get storageKey() {
return this.getAttribute('storage-key');
}
connectedCallback() {
this.textContent = this.quantity; // 初始化显示
this.count();
}
count = () => {
let qty = this.quantity;
if (qty === 0) {
this.textContent = 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);
};
});
</script>
</body>
</html>在上面的示例中:
每个<stock-counter>实例都将独立运行,互不干扰,并且带有storage-key的实例在页面刷新后会从上次保存的状态继续倒计时。
通过采用Web Components的自定义元素,我们优雅地解决了同一页面上多个独立库存计数器的实现难题,提供了一个可复用、可维护且高性能的解决方案。
以上就是实现页面多处独立库存计数器:使用Web Components的解决方案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号