I wanna run my loop limited time, but how can I keep beginning startTime while loop updating all the time

Hi
Sorry for my title, very hard to explain without code or an example

From the beginning, my goal is to run a while loop max 3 minutes while the condition is true.
All while loop try crushed, so I did with the if statement.
Before adding a time limit, all scripts were working fine, but when I added a time limit, I realized
I was doing something very silly, and I don’t know how to solve that.

At the pre-request tab, I assign startTime with a Date.Now().

const startTime = Date.now();
pm.environment.set("startTime", startTime);

At the post-response tab, I am checking the time difference from startTime and now.
Funny thing, always startTime is dependent on DateNow(), and whenever Ioop runs again, startTime updates. And this is expected.

let startTime = pm.variables.get('startTime')
let currentTime = Date.now();
let elapsedTime = currentTime - startTime;
let timeLimit = 180000; // 3 min = 180 second = 180000 ms

if (elapsedTime < timeLimit) {
    if (MY_CONDITION) {
        pm.execution.setNextRequest(pm.info.requestName);
    } else {
        pm.execution.setNextRequest();
    }
    
} else {
    pm.execution.setNextRequest();
}

P.S. if(elapsedTime < timeLimit && MY_CONDITION) could be better, but my focus for solving the time issue first

so…
Whenever I run the collection, startTIme updating for each iteration,
I wanna keep startTime from the beginning for keeping track of time-should never update during loop :: let elapsedTime = currentTime - startTime; startTIme should be the same all the time

Any help appreciated, if there is any other way to run a loop for 3 min, that’s OK too,
I am open to ideas.

thanks

Create an environment variable for your startTime with no value.

In your script, have an IF statement checking for the existence of the environment variable and if the value is null, get your new startDate.

If the value is not null, then do nothing, and it won’t update the StartTime. (Which appears to be your main issue here).

You will just have to remember to remove any values from the variable before each run or in your post response script set it back to null once the time limit has been reach.

You seem to have at least two redundant pm.execution.setNextRequest() lines in your code in your ELSE condition which won’t do anything as you are not telling it what request to run. You can just remove these, and it should just move onto the next request in the collection.

if(elapsedTime < timeLimit && MY_CONDITION) is the right approach. You just put the code for setting the environment variable back to NULL in the ELSE. One IF statement. One ELSE. As you won’t have any setNextRequest() logic in the ELSE then it will just move onto the next request in the collection or as mentioned set it to null to stop all execution.

1 Like

I knew I was missing something simple
Thanks a lot @michaelderekjones,
its working

After creating pm.environment.set("startTime", null); at the end of the previous call,

At pre-request:

if (pm.variables.get('startTime') == null) {
    startTime = Date.now();
    pm.environment.set("startTime", startTime);
} 

At my post-response:

let startTime = pm.variables.get('startTime')
let currentTime = Date.now();
let elapsedTime = currentTime - startTime;
let timeLimit = 180000; // 3 min = 180 second = 180000 ms
if(elapsedTime < timeLimit && MY_CONDITION) {
    pm.execution.setNextRequest(pm.info.requestName);
} else {
    pm.variables.set("startTime", null);
    pm.execution.setNextRequest();
}