Line_Baslat();
setInterval(function(){ moveLeft(); Line_Baslat(); },4000);
function Line_Tekrar () {
$("#dv_line").animate({width:"0%"},0);
};
function Line_Baslat () {
$("#dv_line").animate({width:"100%"},4000);
};
function refseh () {
setInterval(update,4000);
};
$("#prev").click(function(){
moveLeft();
refresh();
});
$("#next").click(function(){
moveRight();
refresh();
});
setInterval
函数返回该间隔的 ID,您可以使用该 ID 来取消它。例如,在您的案例中
Line_Baslat();
var timeout;
function refresh () {
if (timeout) {
clearInterval(timeout);
}
timeout = setInterval(function(){ moveLeft(); Line_Baslat(); },4000);
};
function Line_Tekrar () {
$("#dv_line").animate({width:"0%"},0);
};
function Line_Baslat () {
$("#dv_line").animate({width:"100%"},4000);
};
$("#prev").click(function(){
moveLeft();
refresh();
});
$("#next").click(function(){
moveRight();
refresh();
});
尝试将setInterval
体存储在另一个函数中,如下所示:
//doStuff variables.
var a = 0;
function doStuff(){
//interval body
}
var interval;
interval = setInterval(function(){
doStuff(); //Call Interval Body
},timeout);
clearInterval(interval); //clear interval
//reset interval to call body function
interval = setInterval(function(){
doStuff(); //Call Interval Body
},timeout);
这允许您重新启动/重置间隔。