我试图停止一个循环,这样其他东西就可以运行了,然后在序列中的正确时间再次返回到循环。我的代码运行了一次,但在我使用clearInterval();之后似乎无法运行
function runPlanner() {
var plannerInterval = setInterval(function(){
imageCounter++;
var imageTotal = $('li img').length;
if (imageCounter > imageTotal + 1) {
$('li img').hide();
imageCounter = 0;
clearInterval(plannerInterval);
} else {
$('li img:nth-child(' + imageCounter +')').fadeIn('slow');
}
console.log('image counter:',imageCounter);
console.log('image total:',imageTotal);
}, 2500);
}然后我可以开始运行这个程序,使用以下命令:
runPlanner();但是如果满足条件,在调用clearInterval的地方,则使用该函数调用不起作用。有什么想法吗?
发布于 2013-10-03 22:10:33
尝试使用setTimeout样式。这样你就不会再有clearInterval的问题了。
就像这样。
function runPlanner() {
imageCounter++;
var imageTotal = $('li img').length;
if (imageCounter > imageTotal + 1) {
$('li img').hide();
imageCounter = 0;
} else {
$('li img:nth-child(' + imageCounter +')').fadeIn('slow');
// re-called `runPlanner` after 2500 sec
setTimeout(runPlanner, 2500);
}
console.log('image counter:',imageCounter);
console.log('image total:',imageTotal);
}
runPlanner();https://stackoverflow.com/questions/19161162
复制相似问题