如何动态重新配置setTimeout

时间:2011-07-18 07:37:10

标签: javascript settimeout

我想知道是否有办法将setTimeout函数动态设置为不同的超时。

所以,例如我每隔1秒检查一次,但是如果我没有在10秒内得到预期的结果,我想“重新配置”这个setTimeout等待3秒而不是1秒。

这是我的代码:

var br = 0;
var waitInterval = 1000;
var sleepInterval = 2000;

var waitForNewRace = setInterval(
function checkForNewRace(){
    if ( $("#data").html() == "1"){        
        $("#res").html("got it!");
    }
    else{
        $("#counter").html(br);
        if (br++ > 9)
            waitInterval = 3000;            
    }                   

    $("#tst").html(waitInterval);
},
waitInterval
); 

如果你想查看这里是jsfiddle上提到的代码:http://jsfiddle.net/Hitman666/Vyczj/2/

4 个答案:

答案 0 :(得分:1)

我认为这会做你想做的事情:

var br = 0;

var waitForNewRace = setInterval(function(){
        checkForNewRace();
        if(++br == 9){
            clearInterval(waitForNewRace);
            setInterval(checkForNewRace, 3000);
        }
    }, 1000); 

function checkForNewRace(){
    if($("#data").html() == "1"){   
        $("#res").html("got it!");
        clearInterval(waitForNewRace);
    }else
        $("#counter").html(br);
    $("#tst").html(waitInterval);
}

答案 1 :(得分:1)

您必须停止间隔并重新启动它。请参阅jsfiddle的this fork

编辑:我已经在这里复制了你的代码,以防你的jsfiddle代码出现问题:

var br = 0;
var waitInterval = 1000;
var sleepInterval = 2000;


function checkForNewRace(){
    if ( $("#data").html() == "1"){        
        $("#res").html("got it!");
    }
    else{
        $("#counter").html(br);
        if (br++ > 5){
            clearInterval(waitForNewRace);
            waitInterval += 1000;
            if (waitInterval > 10000)
                waitInterval = 10000;

            waitForNewRace = setInterval(
                 checkForNewRace,
                 waitInterval
            );  
        }
    }                   
    $("#tst").html(waitInterval);
}

var waitForNewRace = setInterval(
    checkForNewRace,
    waitInterval
);         

答案 2 :(得分:0)

您可以使用clearInterval:

var br = 0;
var waitInterval = 1000;
var sleepInterval = 2000;

var waitForNewRace = setInterval(checkForNewRace,
    waitInterval
);    




function checkForNewRace(){
    console.log(waitForNewRace);
    if ( $("#data").html() == "1"){        
        $("#res").html("got it!");
    }
    else{
        $("#counter").html(br);
        if (br++ > 9){
            waitInterval = 3000;            
            clearInterval(waitForNewRace);
            br = 0;


               waitForNewRace = setInterval(checkForNewRace,
    waitInterval
);         
            }
        }                   
    $("#tst").html(waitInterval);
}

小提琴:http://jsfiddle.net/Vyczj/5/

答案 3 :(得分:0)

另一种方法是,只需要在自己的代码中添加两行:

var br = 0;
var waitInterval = 1000;
var sleepInterval = 2000;

var waitForNewRace = setInterval(
function checkForNewRace(){
    if ( $("#data").html() == "1"){        
        $("#res").html("got it!");
    }
    else{
        $("#counter").html(br);
        if (br++ > 9){
            clearInterval(waitForNewRace);
            waitInterval = 3000;          
            waitForNewRace=setInterval(checkForNewRace,waitInterval);  
        }
    }                   

     $("#tst").html(waitInterval);
   },
   waitInterval
);