标签:计时器 start ati int math 计数 指定 set const
const counter = (selector, start, end, step = 1, duration = 2000) => { let current = start, _step = (end - start) * step < 0 ? -step : step, timer = setInterval(() => { current += _step; document.querySelector(selector).innerHTML = current; if (current >= end) document.querySelector(selector).innerHTML = end; if (current >= end) clearInterval(timer); }, Math.abs(Math.floor(duration / (end - start)))); return timer; }; // 事例 counter(‘#my-id‘, 1, 1000, 5, 2000); // 让 `id=“my-id”`的元素创建一个2秒计时器
标签:计时器 start ati int math 计数 指定 set const
原文地址:https://www.cnblogs.com/wkk2020/p/12515134.html