Search code examples
javascriptloopstimerbreakimacros

Using javascript how to break the while loop after a set time?


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

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.


Solution

  • EDIT: This may not be the correct answer...please see comments


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