# Wait for confirmation in for loop

**URL:** <https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480>\
**Category:** Ignition\
**Tags:** ignition81, perspective, vision\
**Created:** [December 22, 2023, 9:18pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480 "2023-12-22T21:18:02Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfan](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jfan](https://forum.inductiveautomation.com/u/jfan)\
**Post date:** [December 22, 2023, 9:18pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/1 "2023-12-22T21:18:02Z")

</div>

Hi All,

Does anyone know how to handle confirmation in a for loop in Perspective?

In Vision, I believe the system.gui.messageBox in a for loop would pause the for loop execution until the user presses the confirm button.

Is there a way in Perspective to handle this? Does using **system.util. invokeAsynchronous** and **time.sleep** a good enough workaround? so that users have enough time to reaction before the for loop continues.

I appreciate any input! Happy Holiday!

---

<div class="post-metadata">

**Author:** ![Tyler\_Shoemake](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/tyler_shoemake/32/55661_2.png) [@Tyler\_Shoemake](https://forum.inductiveautomation.com/u/Tyler_Shoemake)\
**Post date:** [December 22, 2023, 10:09pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/2 "2023-12-22T22:09:48Z")

</div>

From what I have learned, using sleep in UI is bad practice. In vision, calling the sleep function from the UI thread will cause anything else in the UI thread to freeze. I'm not sure of the behavior in perspective, but I assume its the same. I believe the same idea would be applied for messageBox too.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 22, 2023, 10:27pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/3 "2023-12-22T22:27:54Z")

</div>

Calling sleep in Perspective won't lock the browser, but it does your tie up a thread, so the general recommendation to avoid it still applies.

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [December 23, 2023, 11:28am UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/4 "2023-12-23T11:28:51Z")

</div>

For this I would use a message handler.  
On the button that launches the confirmation, add a message handler that does your actions after confirmation. The button opens the confirmation popup and the accept button calls the message handler

---

<div class="post-metadata">

**Author:** ![jfan](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jfan](https://forum.inductiveautomation.com/u/jfan)\
**Post date:** [December 23, 2023, 3:02pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/5 "2023-12-23T15:02:21Z")

</div>

If it’s just one confirmation, the message handler will work fine. But this is in a for loop, which means it could have multiple confirmations. So how can I pause the for loop execution for each confirmation?

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 23, 2023, 5:53pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/6 "2023-12-23T17:53:27Z")

</div>

> [@jfan](#):
>
> ... **time.sleep** a good enough workaround? so that users have enough time to reaction before the for loop continues.

That would be a terrible solution. If the user gets distracted the loop will continue.

- Create a custom variable on the view. Call it `acknPointer : 0` or similar.
- Put your confirmation loop script in an `onChange` event on `acknPointer`. Modify it to do the popup on change to a non-zero value and just `return` without doing anything if it == 0.
- Modify the popup to accept the `acknPointer` variable as a parameter. On pressing the OK button increment the `acknPointer` and pass it back in the sendMessage payload.
- Use the message listener to copy the payload's `acknPointer` to the view's custom property which will increment it by one and retrigger the `onChange` script.
- On the last acknowledge set `ackPointer` to zero which will cause the `onChange` to abort early.

---

<div class="post-metadata">

**Author:** ![jfan](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jfan](https://forum.inductiveautomation.com/u/jfan)\
**Post date:** [December 23, 2023, 7:42pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/7 "2023-12-23T19:42:21Z")

</div>

Thank you! I think the while loop will be able to make it work with the method you mentioned here.

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 23, 2023, 8:01pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/8 "2023-12-23T20:01:10Z")

</div>

> [@jfan](#):
>
> I think the while loop will be able to make it work ...

No it won't. Don't do that because something will have to hang while waiting for a response. This would be a really bad design decision.

My solution runs a sequence of events which respond to triggering actions - either `onChange` or `sendMessage`. There is no loop. There is no wait or sleep. There is no while. Each time a change occurs the code executes and exits. Read it again!

---

<div class="post-metadata">

**Author:** ![jfan](https://avatars.discourse-cdn.com/v4/letter/j/e8c25b/32.png) [@jfan](https://forum.inductiveautomation.com/u/jfan)\
**Post date:** [December 23, 2023, 8:06pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/10 "2023-12-23T20:06:28Z")

</div>

Okay I see what you mean. I’ll give it a try. Basically I will be tracking the for loop index with the custom property and execute the script that way

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [December 23, 2023, 8:28pm UTC](https://forum.inductiveautomation.com/t/wait-for-confirmation-in-for-loop/83480/11 "2023-12-23T20:28:34Z")

</div>

Yes. I should have said that your script will go into acknPointer's property change script. (Right-click on the custom property.)

Let's say you have five checks to do then set `acknPointer = 1`.

- That will trigger acknPointer's Property Change script which will open the popup (and exit the Property Change script).
- When the user responds to the popup use `sendMessage` to send '2' to the message handler on `acknPointer`. The listener will update the pointer and do whatever has to be done with that row of the dataset. Also close the popup (by a line in the script - not by a navigation event as these running both a script and navigation in the same event can cause problems do to executing in the wrong order).
- The change in `acknPointer` will trigger the Property Change event on the custom prop.

Each bit of code does the job and exits.
