Javascript 滑块函数不';我没有足够的时间快速点击

Javascript 滑块函数不';我没有足够的时间快速点击,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我使用JQuery创建了一个简单的滑块。它使用以下功能工作: function moveleft(){ if (current < $length - 1){ $tape.animate({"margin-left":"-=" + $width}, $speed, function(){ current++; }) }else if(current == $length - 1){ $tape.css("margin-left", 0);

我使用JQuery创建了一个简单的滑块。它使用以下功能工作:

function moveleft(){
if (current < $length - 1){
    $tape.animate({"margin-left":"-=" + $width}, $speed, function(){
        current++;
        })
}else if(current == $length - 1){
    $tape.css("margin-left", 0);
    current = 0;
    $tape.animate({"margin-left":"-=" + $width}, $speed, function(){
        current++;  
    })
}
};

function moveright(){
if (current == 0){
    $tape.css("margin-left", -$last);
    current = $length - 1;
    $tape.animate({"margin-left":"+=" + $width}, $speed, function(){
        current--;  
    })
}else if (current > 0){
    $tape.animate({"margin-left":"+=" + $width}, $speed, function(){
        current--;
        })
}
}
函数moveleft(){
如果(当前<$length-1){
$tape.animate({“左边距”:“-=”+$width},$speed,function(){
电流++;
})
}else if(当前==$length-1){
$tape.css(“左边距”,0);
电流=0;
$tape.animate({“左边距”:“-=”+$width},$speed,function(){
电流++;
})
}
};
函数moveright(){
如果(当前==0){
$tape.css(“左边距”-$last);
当前=$length-1;
$tape.animate({“左边距”:“+=”+$width},$speed,function(){
当前--;
})
}否则如果(当前>0){
$tape.animate({“左边距”:“+=”+$width},$speed,function(){
当前--;
})
}
}
演示:


它可以工作,但问题是当用户点击按钮太快时,它可能没有足够的时间来计算值。所以,一切都是向左或向右发展。我可以添加什么来修复它吗?

您可以将函数包装在一个Is动画检查中,就像这样…
这将确保动画在再次单击之前完成,并执行任何操作

function moveleft(){
    if( $(tape).is(':animated') ) {return}
    else{
    if (current < $length - 1){
        $tape.animate({"margin-left":"-=" + $width}, $speed, function(){
            current++;
            })
    }else if(current == $length - 1){
        $tape.css("margin-left", 0);
        current = 0;
        $tape.animate({"margin-left":"-=" + $width}, $speed, function(){
            current++;  
        })
    }
    }

    };
函数moveleft(){
if($(磁带).is(':animated')){return}
否则{
如果(当前<$length-1){
$tape.animate({“左边距”:“-=”+$width},$speed,function(){
电流++;
})
}else if(当前==$length-1){
$tape.css(“左边距”,0);
电流=0;
$tape.animate({“左边距”:“-=”+$width},$speed,function(){
电流++;
})
}
}
};
.off()
单击箭头时的单击事件,以及
.on()
动画结束后的单击事件