# Better solution for a delay in a script

**URL:** <https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913>\
**Category:** Ignition\
**Tags:** ignition81, perspective\
**Created:** [March 6, 2024, 4:59pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913 "2024-03-06T16:59:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zachary\_Kacel1](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zachary\_Kacel1](https://forum.inductiveautomation.com/u/Zachary_Kacel1)\
**Post date:** [March 6, 2024, 4:59pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913/1 "2024-03-06T16:59:31Z")

</div>

I have a scenario in which I am building a perspective page that does not use any tags. A user will scan a barcode or manually enter it, triggering a few events such as fetching drawing data from our drawing web service and collecting product information from our ERP system via a web service. All of these actions are initiated from an on-change script on an order number field.

Everything is working as intended, except for one aspect. The team leads would like the screen to change even if the same order number is scanned. Due to everything being in a change script, nothing happens when the same value is entered. Therefore, I am attempting to design the system so that if the same number is scanned, the following actions occur:

1. Clear the order number value (which triggers the change script, setting values to blank).
2. Wait for 3 seconds.
3. Re-enter the order number(triggering the on-change script to execute all the necessary events for pulling drawings and product information.)

I have something working using a `time.sleep`, but I know that this is not the correct way to achieve this. What would be a suitable solution for implementing a timer that clears the values, waits for 3 seconds, and then re-enters the order number?

My current solution involves `order_number_prep`, which writes directly to my `order_number`. If the values don't match, it writes to the `order_number`. If they do match, it clears the field for 3 seconds and then writes the number back.

```auto
def valueChanged(self, previousValue, currentValue, origin, missedEvents):
	import time
	if len(str(currentValue.value)) > 0:
		if str(currentValue.value) != str(self.custom.order_number): 
			self.custom.order_number = currentValue.value
			self.custom.order_number_prep = ''
		else:
			self.custom.order_number = ''
			time.sleep(3)
			self.custom.order_number = currentValue.value
			self.custom.order_number_prep = ''

```

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/f/6fa9cbbd961723995e6caed6704ece1594bed558.png)

---

<div class="post-metadata">

**Author:** ![cmallonee](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmallonee/32/6416_2.png) [@cmallonee](https://forum.inductiveautomation.com/u/cmallonee)\
**Post date:** [March 6, 2024, 5:01pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913/2 "2024-03-06T17:01:57Z")

</div>

Please view this recent thread for suggested approaches: [Alternative to sleep](https://forum.inductiveautomation.com/t/alternative-to-sleep/86843)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 6, 2024, 5:09pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913/3 "2024-03-06T17:09:23Z")

</div>

Instead of clearing the order number, consider simply incrementing an integer custom property. Include that integer in dependent bindings, but in a way that isn't significant. (Like an extra query param in URLs that just gets around browser caching.)

That'll make all of your bindings re-evaluate in spite of no actual value change.

---

<div class="post-metadata">

**Author:** ![Zachary\_Kacel1](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zachary\_Kacel1](https://forum.inductiveautomation.com/u/Zachary_Kacel1)\
**Post date:** [March 6, 2024, 5:12pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913/5 "2024-03-06T17:12:04Z")

</div>

Thanks.  
I used your solution where I invokeAsynchronous and put my sleep in that.

```auto
import time
def update():
	self.custom.order_number = ''
	time.sleep(2)
	self.custom.order_number = currentValue.value
	self.custom.order_number_prep = ''
	
if len(str(currentValue.value)) > 0:
	if str(currentValue.value) != str(self.custom.order_number): 
		self.custom.order_number = currentValue.value
		self.custom.order_number_prep = ''
	else:
		system.util.invokeAsynchronous(update)

```

---

<div class="post-metadata">

**Author:** ![Zachary\_Kacel1](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zachary\_Kacel1](https://forum.inductiveautomation.com/u/Zachary_Kacel1)\
**Post date:** [March 6, 2024, 5:14pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913/6 "2024-03-06T17:14:32Z")

</div>

I didn't think of that. I have used something similar in a webdev resource to increment a parameter to force a refresh.

Thanks

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 6, 2024, 5:34pm UTC](https://forum.inductiveautomation.com/t/better-solution-for-a-delay-in-a-script/86913/7 "2024-03-06T17:34:20Z")

</div>

> [@Zachary\_Kacel1](#):
>
> increment a parameter

It has the advantage of needing no delay at all, so the UI is much more responsive.
