Forum Discussion

hhagay's avatar
hhagay
Contributor
8 years ago

Breaking out of a while loop in JS

Hello Community

 

Simple put, I'd like to iterate number of times and if the condition is not met, break out. I am using while to allow the object to load (nothing else works....).

 

 

What is wrong with this code?

 

var iter = 2; 

link = page.Find("contentText", name, 10);
while (!link.Exists)
{
var i = function iReturn(result)
{
for(i==0; i<=iter; i++)
{
aqUtils.Delay(ms, HelpStr);
return result;
}
}
if (i==iter) break;
}

2 Replies

  • NisHera's avatar
    NisHera
    Valued Contributor

    first of all your function is not organized.
    it should be clear to other person what you are trying to do.


    second I do not recommend Using while for wait a object to appear
    If object not appeared it will wait for ever without moving to next step.


    third in side while loop you do nothing but assigning variable "i"
    (if iReturn is checking exist of page it's OK)
    see the link for while loop example.


    forth your for loop is not correct
    please look at my suggestion below

     

    fifth it is not recommended to use Name property to find an object 

     

    function ABCD(){
      for(i=0;i<150;i++){
         var link = page.Find("contentText", name, 10);
    delay(500); if (link.Exists){ break; } } }
    • hhagay's avatar
      hhagay
      Contributor

      Hello and thank you for all the valuable tips

       

      This code snippet achieves the intended goal. Please let me know your thoughts.

       

      Also - what do you suggest to use to find object (if none of the usual work)?

       

      while(!crewMemberLink.Exists)
      {
      if (i==iter)
      {
      break;
      }
      for (i==0; i<iter; i++)
      {
      aqUtils.Delay(ms, HelpStr+" ["+i+"]");
      }
      }