The logic is exactly the same as with Arduino or ESP
One way is to code your function to respond to Button HIGH press, start timeout timer and process your command…
Then when timeout timer "times out " have it send LOW to widget and run a sync command…
Then when the function is called to respond to LOW, do whatever you need to stop your processed command.