Forum Discussion

shahid24's avatar
shahid24
Contributor
11 months ago
Solved

How to perform certain actions for a specific amount of time in a loop

I want to perform certain action inside a Do Loop statement, in this case i want to ensure that i get out of the loop after ten minutes have elapsed. What should be the MaxDelay+X value ?  Sub Test ...
  • MW_Didata's avatar
    11 months ago

    I have this function that seems to do what you want, It is written in Javascript but rewriting to VB should not be a problem.

    function DoStuff(delayMS)
    {
    let start = Date.now();
      
      while(Date.now() - start < delayMS)
      {
        if (Aliases.Stuff.Exists)
        {
          Delay(500);
          Aliases.Stuff.Close(1000);
          return;
        }
        Delay(100); //Short break before checking again
      }
    
    Log.Warning("Stuff did not exist!");
    }

    The function checks if something exists and closes it, It waits for a max of {delayMS} miliseconds 

    If it doesn't exists within the given time it posts a warning to the logs

  • Hassan_Ballan's avatar
    11 months ago

    Doing something have x amount of time that can not be calculated. This JavaScript code would be more realistic to expectations.

    function TimeIt()
    {
      Now = new Date().getTime();
      ExitAt = Now + 600000; // add 10 minutes in milleseconds
      while (Now < ExitAt) {
        // Do something
        Now = new Date().getTime();
      }
    }