我设置了这个简单的幻灯片,如果我删除参数并手动添加类和速度,一切都会正常。
如果我使用参数来设置类别和速度,则会失败。随着第一张图像应用当前类,然后 dom 似乎变得疯狂。
虽然控制台中没有错误,但'undefined'弹出很多。
如有任何帮助,我们将不胜感激。谢谢
let pos = 0;
window.addEventListener("load", function(event) {
testIt('current', 5000);
});
function testIt(_class, _speed) {
const testPara = document.querySelectorAll('.bg_imgs');
let i;
for(i = 0; i < testPara.length; i++) {
testPara[i].classList.remove(_class);
}
pos++;
if(pos > testPara.length) {pos = 1;}
testPara[pos-1].classList.add(_class);
setTimeout(_class, _speed); }
最佳答案
您似乎向 setTimeout
发送了错误的参数。它应该采用 setTimeout(function, time)
的形式,但相反,您传递的是我认为是字符串的内容。
setTimeout(function() { testIt(_class, _speed); }, _speed);
应该可以
关于JavaScript函数参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48964650/