使用javascript如何在设定的时间后打破while循环? [英] Using javascript how to break the while loop after a set time?

查看:237
本文介绍了使用javascript如何在设定的时间后打破while循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个while循环,看起来像这样:

I have a while loop which looks like this:

while(s1 != "#EANF#")
{
iimPlay("CODE:REFRESH");
iimPlay("CODE:TAG POS=1 TYPE=* ATTR=TXT:Contacted:* EXTRACT=TXT")
var s1 = iimGetLastExtract();
}

它将刷新网页,直到找到所需的内容为止.但是有时它变成一个无限循环,我想简单地设置一个计时器,这样它就可以打破while循环并继续下去.我尝试查看settimeout,但无法弄清楚该如何做.我想让这个while循环只放弃刷新网页,如果它说了3分钟后却找不到所需的内容.

it will refresh the web page until it finds what it wants. However sometimes it becomes an infinite loop and I want to simply set a timer so that it would break the while loop and carry on. I tried looking at settimeout but couldn't figure out how to do it. I want this while loop to just give up refreshing the web page if it doesn't find what it wants after lets say 3 minutes.

推荐答案

这可能不是正确的答案 ...请查看评论

您可以在while循环中使用3分钟后翻转的标志:

You can use a flag that is flipped after 3 minutes in your while loop condition:

var keepGoing = true;
setTimeout(function() {
    // this will be executed in 3 minutes
    // causing the following while loop to exit
    keepGoing = false;
}, 180000); // 3 minutes in milliseconds

while(s1 != "#EANF#" && keepGoing === true)
{
iimPlay("CODE:REFRESH");
iimPlay("CODE:TAG POS=1 TYPE=* ATTR=TXT:Contacted:* EXTRACT=TXT")
var s1 = iimGetLastExtract();
}

这篇关于使用javascript如何在设定的时间后打破while循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆