javascript - jQuery .each 和 setTimeout 不起作用
问题描述
我正在尝试从活动滑块中获取一些 attar vale。我已经使用了 each 和 setTimeout 函数。最初,这行得通。但是如果我使用这段代码,一段时间后滑块会停止。请注意,此代码独立于滑块代码。
function checkForChanges() {
$('#ninja-slider ul li').each(function(i) {
if ($(this).hasClass("ns-show")) {
//$(this).css("color", "red");
console.log('yes')
} else {
setTimeout(checkForChanges, 3000);
console.log('no')
}
});
}
$(checkForChanges);
解决方案
如果您想每 3 秒获得一次活动滑块,则不需要每个滑块,它可能像:
function checkForChanges() {
//Here is the active li in the slide
var active_li = $("#ninja-slider li.ns-show");
//Get the src of shwon image
console.log( $("#ninja-slider li.ns-show").find('img').attr('src') );
setTimeout(checkForChanges, 3000);
}
checkForChanges();
推荐阅读
- jenkins - 在詹金斯管道中,如何访问每个阶段的测试指标?
- python - python中的OpenCV cvtColor问题
- vba - IIF 关于标准的声明
- python - 即使我明确为它分配了不同的值,方法属性也不会自行更新
- javascript - 如何使 html 运行没有开始或结束的代码?
- javascript - JS在生成的ajax div中添加文本
- flutter - Flutter 图像选择器已经激活,null 异常
- go - 在 Go 中使用 stdlib 中未导出的函数/类型
- performance - CSV 迄今为止和浮动
- c++ - 使用 old_pointer + offset 的 new_pointer 设置 shared_ptr