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

**URL:** <https://community.postman.com/t/i-wanna-run-my-loop-limited-time-but-how-can-i-keep-beginning-starttime-while-loop-updating-all-the-time/79198>\
**Category:** Help Hub\
**Tags:** variables, loop\
**Created:** [April 25, 2025, 9:00am UTC](https://community.postman.com/t/i-wanna-run-my-loop-limited-time-but-how-can-i-keep-beginning-starttime-while-loop-updating-all-the-time/79198 "2025-04-25T09:00:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![greenridinghood](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/greenridinghood/32/26145_2.png) [@greenridinghood](https://community.postman.com/u/greenridinghood)\
**Post date:** [April 25, 2025, 9:00am UTC](https://community.postman.com/t/i-wanna-run-my-loop-limited-time-but-how-can-i-keep-beginning-starttime-while-loop-updating-all-the-time/79198/1 "2025-04-25T09:00:41Z")

</div>

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().

```auto
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.

```auto
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

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [April 25, 2025, 5:50pm UTC](https://community.postman.com/t/i-wanna-run-my-loop-limited-time-but-how-can-i-keep-beginning-starttime-while-loop-updating-all-the-time/79198/2 "2025-04-25T17:50:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![greenridinghood](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/greenridinghood/32/26145_2.png) [@greenridinghood](https://community.postman.com/u/greenridinghood)\
**Post date:** [April 25, 2025, 9:08pm UTC](https://community.postman.com/t/i-wanna-run-my-loop-limited-time-but-how-can-i-keep-beginning-starttime-while-loop-updating-all-the-time/79198/3 "2025-04-25T21:08:47Z")

</div>

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:

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

```

At my post-response:

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

```
