# Waiting in a Gateway Script

**URL:** <https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300>\
**Category:** Ignition\
**Created:** [May 6, 2022, 3:05pm UTC](https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300 "2022-05-06T15:05:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim.schultz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@tim.schultz](https://forum.inductiveautomation.com/u/tim.schultz)\
**Post date:** [May 6, 2022, 3:05pm UTC](https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300/1 "2022-05-06T15:05:35Z")

</div>

Hello - I’m looking for best practices for looping in a gateway script without impacting system performance. I am working with a vendor API implementation that looks a bit like this:  
Get Status from API (should be ‘IDLE’)  
Send Input Data to API  
Poll Status from API until it is ‘IDLE’  
Retrieve Output Data from API

Is there a better option than simply using a while loop? I looked at the ‘Adding a Delay to a Script’ help page, but none of the options besides a while loop or sleep allowed me to use the polling feedback as a conditional break out.

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [May 6, 2022, 3:16pm UTC](https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300/2 "2022-05-06T15:16:28Z")

</div>

You can set up a gateway timer script to run and do this every X seconds. Or used a scheduled script to set it on a cron job. In a way Ignition takes care of the while loop, you just need to write the inside of your loop and tell Ignition how often you want to run it.

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [May 6, 2022, 3:33pm UTC](https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300/3 "2022-05-06T15:33:59Z")

</div>

The approaches @bkarabinchak.psi mention are good ones. I’ll supplement that if you might need a more dynamic “sleep” period, I’ve had good luck in the past using something like a timer script that defines your minimum frequency for _checking_ to see if it is time to do work. You could do this by maintaining a `NextActionTime` tag that contains the timestamp after which to do work. Each invocation of the timer script you compare and see if current time exceeds `NextActionTime` and then set `NextActionTime` (to the _next_ time) and do your current work. This helps you prevent a blocking “sleep”. It also gives you a handy way to debug by viewing that tag.

---

<div class="post-metadata">

**Author:** ![tim.schultz](https://avatars.discourse-cdn.com/v4/letter/t/71e660/32.png) [@tim.schultz](https://forum.inductiveautomation.com/u/tim.schultz)\
**Post date:** [May 6, 2022, 3:56pm UTC](https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300/4 "2022-05-06T15:56:42Z")

</div>

Thanks both. I was hoping to avoid using tags at all for managing the state, but it seems like I will need to.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 6, 2022, 4:00pm UTC](https://forum.inductiveautomation.com/t/waiting-in-a-gateway-script/59300/5 "2022-05-06T16:00:17Z")

</div>

If you get an instance of [`GatewayContext`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.16/com/inductiveautomation/ignition/gateway/model/GatewayContext.html) (search the forums, I’m being deliberately vague here), you can get the shared [`ExecutionManager`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.16/com/inductiveautomation/ignition/common/execution/ExecutionManager.html), which allows you to register a [`SelfSchedulingRunnable`](https://files.inductiveautomation.com/sdk/javadoc/ignition81/8.1.16/com/inductiveautomation/ignition/common/execution/SelfSchedulingRunnable.html) which has the same capability Kevin described built in. It would require some moderately advanced scripting know-how, but your scheduled task would also show up on Status → Execution.
