Page 1 of 1

Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 00:06
by viv
Let's say the following, Ctrl_Change only continues after 1 second with no change
Gui_Size continues only after releasing the mouse

Code: Select all

mygui := gui("Resize")
mygui.OnEvent("size", Gui_Size)
test := mygui.add("edit","w400 h30")
test.OnEvent("change",Ctrl_Change)
mygui.show

Ctrl_Change(GuiCtrlObj, *)
{
    MsgBox(GuiCtrlObj.Value)
}

Gui_Size(GuiObj, MinMax, Width, Height)
{
    mygui.GetPos(, ,&w,&h)
    test.Move(, ,w-40,h-50)
}

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 01:59
by TheArkive
I'm afraid I'm not entirely sure of what you are trying to do.

Is Sleep not what you are looking for?

What is your use case for this delay?

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 02:10
by viv
@TheArkive

Ctrl_Change needs to search and then display the data
The process of searching and displaying takes some time
Because each character entered will trigger an event
So I need to add a delay to make sure that the input is complete (no re-entry within a second )


The Gui_Size event is similar, as it requires redrawing the interface, the interface will lag and flicker slightly.
I want to redraw the interface after the mouse is released

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 02:19
by TheArkive
In the case of Ctrl_Change, as you know, it fires with every keystroke, but you are looking for some other kind of event. The script will have no concept of what "complete input" looks like unless you code in those parameters. One (old) way of determining "complete input" is to wait for the user to press {ENTER}. You can scan the edit text on every keystroke. When `n is detected, then you can execute whatever your code is (unless your incomplete input is intended to contain `n).

I'm not sure about Gui_Size at the moment. You may be able to set some window styles to achieve your desired resize effect.

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 02:43
by viv
@TheArkive
Ctrl_Change
I am currently removing the change event from edit
and then added a hidden default button and bind it to this
which is the response to Enter


It seems I'm overthinking it, so maybe

Code: Select all

Ctrl_Change(GuiCtrlObj, *)
{
	static TimeSave := ""
	if TimeSave
	ToolTip A_TickCount-TimeSave
	if TimeSave && A_TickCount-TimeSave > 1000
		MsgBox(GuiCtrlObj.Value), TimeSave := ""
	Else 
		TimeSave := A_TickCount
}

Still not quite right, the last input will not trigger if it is within one second

I still have to find a way to determine if it's the last input ....

Re: Is there an easy way to add a delay to an event?  Topic is solved

Posted: 21 Oct 2021, 04:57
by swagfag

Code: Select all

#Requires AutoHotkey v2.0-beta.2

G := Gui()

Edit1 := G.Add('Edit')
Edit1.OnEvent('Change', (*) => SetTimer(showWhatYouTypedAfter1s, -1000))
showWhatYouTypedAfter1s() => ToolTip(Edit1.Value)

G.Show()

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 05:15
by viv
@swagfag

Thank you very much
I have tried this method before and it seems that the SetTimer function is repeated,
However, the documented SetTimer function does not run repeatedly
I think I wrote it wrong

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 07:13
by kczx3
Wouldn’t you want to track the timer and destroy it if another change event occurs within that one second?

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 10:39
by viv
@kczx3

Yes, I used to write the same thing probably?
But the function called is repeated every time the event is triggered
And the answer way I tested other things like the msgbox fileapped would be destroyed before execution
So the last SetTimer within one second is destroyed by the current SetTimer, until the last one is triggered after exactly -1000ms.
I think I should have written it wrong before

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 11:11
by kczx3
I can’t quite follow your English, apologies. I think what you’re saying is that only one timer can be active per function object. Is that right?

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 11:19
by 20170201225639
until the last one is triggered after exactly -1000ms
possibly has to do with this
If Period is 0, the timer is marked for deletion. 👉 If a thread started by this timer is still running 👈, the timer is deleted after the thread finishes (unless it has been reenabled); otherwise, it is deleted immediately. In any case, the timer's previous Period and Priority are not retained.

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 11:28
by kczx3
Nice, that's handy and good to know!

Re: Is there an easy way to add a delay to an event?

Posted: 21 Oct 2021, 11:32
by viv
@kczx3
sorry for my online translation in English..
One timer, multiple runs
The answer is correct, I have no problem now, I wrote it wrong before

Code: Select all

SetTimer test,-1000 ;1
SetTimer test,-1000 ;2 destroy 1
SetTimer test,-1000 ;3 destroy 2
SetTimer test,-1000 ;4 destroy 3
SetTimer test,-1000 ;5 destroy 4,runthis


test()
{
	MsgBox 1
}