Better solution for a delay in a script

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.

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

Please view this recent thread for suggested approaches: Alternative to sleep

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.

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

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)

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

Thanks

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