jQuery .each 和 setTimeout 不工作
jQuery .each and setTimeout is not working
我正在尝试从活动滑块中获取一些 attar 值。我已经使用了 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();
我正在尝试从活动滑块中获取一些 attar 值。我已经使用了 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();