setinterval只执行一次动画,这个是轮播动画,为什么我调用setinterval只滑动一次,而我调用2次setinterval就能正常自动轮换

qq_42545457 2019-04-08 10:19:14
function nextPage(next=true){
if(moving){
return
}
moving=true;
var currentW= $list.position().left
var offset= next? -IMG_WIDTH:IMG_WIDTH;
if(currentW==0){
currentW=-(listCount-2)*IMG_WIDTH
$list.css("left",currentW)
}else if(currentW==-(listCount-1)*IMG_WIDTH){
currentW=-IMG_WIDTH
$list.css("left",currentW)
}
$list.animate({left:currentW+offset+"px"},"slow",function(){
moving=false
});
updatePoints(next);
}

setInterval(function(){
nextPage();
},1000);
...全文
132 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
2019-04-09
  • 打赏
  • 举报
回复
animate 和 setInterval的时间线没有同步,在调用nextPage时,moving还是true就返回了

function nextPage(next = true) {
    if (moving) {
        return
    }
    moving = true;
    var currentW = $list.position().left
    var offset = next ? -IMG_WIDTH: IMG_WIDTH;
    if (currentW == 0) {
        currentW = -(listCount - 2) * IMG_WIDTH
        $list.css("left", currentW)
    } else if (currentW == -(listCount - 1) * IMG_WIDTH) {
        currentW = -IMG_WIDTH
        $list.css("left", currentW)
    }
    $list.animate({
        left: currentW + offset + "px"
    }, "slow", function() {
        moving = false;
		setTimeout(function() {
			nextPage();
		}, 400);
    });
    updatePoints(next);
}

setTimeout(function() {
    nextPage();
}, 1000);

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧