Javascript 问题是同一页上有多个计数器

Javascript 问题是同一页上有多个计数器,javascript,html,Javascript,Html,我的柜台号码有问题。我需要有几个在同一页上,但如果一个上传另一个不再,你能帮我吗 var str=“@idistagram”; var n=str.length; var-lungh=n+750; 函数animateValue(对象、开始、结束、持续时间){ 让startTimestamp=null; 常量步骤=(时间戳)=>{ 如果(!startTimestamp)startTimestamp=时间戳; const progress=Math.min((时间戳-startTimestamp)

我的柜台号码有问题。我需要有几个在同一页上,但如果一个上传另一个不再,你能帮我吗

var str=“@idistagram”;
var n=str.length;
var-lungh=n+750;
函数animateValue(对象、开始、结束、持续时间){
让startTimestamp=null;
常量步骤=(时间戳)=>{
如果(!startTimestamp)startTimestamp=时间戳;
const progress=Math.min((时间戳-startTimestamp)/持续时间,1);
obj.innerHTML=Math.floor(进度*(结束-开始)+开始);
如果(进度<1){
window.requestAnimationFrame(步骤);
}
};
window.requestAnimationFrame(步骤);
}
const obj=document.getElementById('primo');
动画评估(obj,0,lungh,2000)

使用类作为选择器,然后您可以从HTML集合创建数组,并为每个类启动您的方法

var str=“@idistagram”;
var n=str.length;
var-lungh=n+750;
函数animateValue(对象、开始、结束、持续时间){
让startTimestamp=null;
常量步骤=(时间戳)=>{
如果(!startTimestamp)startTimestamp=时间戳;
const progress=Math.min((时间戳-startTimestamp)/持续时间,1);
obj.innerHTML=Math.floor(进度*(结束-开始)+开始);
如果(进度<1){
window.requestAnimationFrame(步骤);
}
};
window.requestAnimationFrame(步骤);
}
var counters=document.getElementsByClassName('primo');
from(counters).forEach(el=>animateValue(el,0,lungh,2000))


查找逻辑基于id。如果要对多个元素重复此操作,则需要切换到使用另一个选择器,该选择器允许repetition@Taplar,谢谢你的回复,你能告诉我怎么办吗?我应该使用querySelector吗?我是js新手,所以我不理解一些概念。SID选择器只返回第一个匹配的元素,每个元素在页面上必须有唯一的id…使用类选择器,你可以编辑你的可运行代码段,使其与单个元素的工作方式相同吗?我想我们知道为什么它在你的上一个问题中不起作用了…最好的答案!为我工作,谢谢!