如何动态重新配置 setTimeout

发布于 2024-11-24 20:48:11 字数 714 浏览 1 评论 0原文

我想知道是否有一种方法可以将 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/

I was wondering if there is a way to set the setTimeout function to different timeout on the fly.

So, for example I check something every 1 second, but if I don't get an expected result in like 10 seconds I want to "reconfigure" this setTimeout to wait 3 seconds instead of 1.

Here is my code:

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

If you want to check it out here is the mentioned code on jsfiddle: http://jsfiddle.net/Hitman666/Vyczj/2/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短暂陪伴 2024-12-01 20:48:11

您必须停止间隔并重新开始。请参阅 jsfiddle 的此分支

编辑:我已经在这里复制了您的代码,以防您的 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
);         

You have to stop the interval and restart it. See this fork of your jsfiddle.

EDIT: I've copied your code here in case something happens with your jsfiddle code:

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
);         
浮萍、无处依 2024-12-01 20:48:11

我认为这会做你想要的:

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

I think this will do what you want:

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);
}
掩饰不了的爱 2024-12-01 20:48:11

您可以使用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);
}

fiddle:http://jsfiddle.net/Vyczj/5/

You could use 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);
}

fiddle: http://jsfiddle.net/Vyczj/5/

可是我不能没有你 2024-12-01 20:48:11

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

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

An alternative, just need to add two lines to your own code:

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
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文