如何重启setinterval

时间:2015-01-19 20:57:38

标签: javascript setinterval

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();
});    

2 个答案:

答案 0 :(得分:0)

setInterval函数返回该间隔的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();
});  

答案 1 :(得分:0)

尝试将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); 

这允许您重新启动/重置间隔。