超出最大调用堆栈大小,试图在我的 div 中计算值

Maximum call stack size exceeded, trying to make counting up value in my div

正在尝试将 div 中的 innerText 从 0 更改为 div 中的值。 你能告诉我 - 为什么调用堆栈大小超过了,我试图创造条件来离开我的递归 - 但它不起作用

const itemsToCount = document.querySelectorAll('.customers'); 


function counter (item) {
    let stopPoint = Number(item.innerText.slice(0,-1));
    let counterNum = 0; 
    function increase () {  
        if (counterNum === stopPoint) {
            item.innerText = `${item}+`
            return; 
        } else {
            setTimeout(() => {
                item.innerText = counterNum; 
                
            }, 100)
            counterNum += 1;
            return increase(); 
        }
        
    }
    increase()
}

Array.from(itemsToCount).forEach(item => {
    counter(item)
})

我的HTML长得像

                 <div class="wrapper">
                      <p class="experience__counter customers">250+</p>
                      <p class="experience__counter customers">20+</p>
                      <p class="experience__counter customers">150+</p>

                    </div>
                  </div>

嘿,根据我的说法,querySelectorAll returns 是一个集合,因此您不能像这样更改其 innerText。你必须使用这个——itemsToCount[0] 这里 0 是索引 try using this

“尝试将 counterNum += 1; increase(); 放入 setTimeout 回调中” Bravo 2 天前

谢谢,https://whosebug.com/users/10549313/bravo