Script not running at the told speed

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Zseni
Posts: 53
Joined: 03 Nov 2019, 19:44

Script not running at the told speed

15 Nov 2019, 20:59

So I have this script, it works but it doesn't run fast enough. This should take 3 seconds but it takes 9 seconds instead.
The script should count down in milliseconds from 3000 to 0.

Code: Select all

#NoEnv
#SingleInstance, force
CountDown = 3000

UpdateTimer:
ToolTip, Starting %CountDown%
CountDown -= 1
if (CountDown > 0)
	{
                SetTimer, UpdateTimer
	}
	else {
		ToolTip
    }
return

F4::
ExitApp
return
It's a simple issue but I have no idea how to fix it.
Thank you in advance.
User avatar
boiler
Posts: 16957
Joined: 21 Dec 2014, 02:44

Re: Script not running at the told speed

15 Nov 2019, 22:15

It's not so simple to fix. There's really no reason to think it should take 3 seconds to complete. It would be quite a coincidence if each iteration happened to take exactly 1 ms. Your script isn't doing anything to count how much time has elapsed. In fact, you won't be able to get it to do so because each AHK command will take at least a few ms to execute. You could have it count down from 3 seconds, but you would have to do it in much larger than 1 ms increments, and you'd have to check the elapsed time in each iteration (via A_TickCount, for example), not just assume how much time has passed.
User avatar
Zseni
Posts: 53
Joined: 03 Nov 2019, 19:44

Re: Script not running at the told speed

16 Nov 2019, 00:02

boiler wrote: It's not so simple to fix. There's really no reason to think it should take 3 seconds to complete. It would be quite a coincidence if each iteration happened to take exactly 1 ms. Your script isn't doing anything to count how much time has elapsed. In fact, you won't be able to get it to do so because each AHK command will take at least a few ms to execute. You could have it count down from 3 seconds, but you would have to do it in much larger than 1 ms increments, and you'd have to check the elapsed time in each iteration (via A_TickCount, for example), not just assume how much time has passed.
I thought that was the case, but I still asked just in case there was a fix like a #nokeydelay?? or something.
Following what you said I will make it just go down in whole seconds.
Thank you for your response and for clearing this up for me.
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Script not running at the told speed

16 Nov 2019, 06:10

Read here: SetTimer
PeriodOnOffDelete

Default: If this parameter is blank and:
1) the timer does not exist: it will be created with a period of 250.
Timer precision: Due to the granularity of the OS's time-keeping system, Period is typically rounded up to the nearest multiple of 10 or 15.6 milliseconds (depending on the type of hardware and drivers installed). For example, a Period between 1 and 10 (inclusive) is usually equivalent to 10 or 15.6 on Windows 2000/XP. A shorter delay may be achieved via Loop+Sleep as demonstrated at DllCall+timeBeginPeriod+Sleep.
User avatar
SuperFoobar
Posts: 83
Joined: 23 Nov 2018, 15:14

Re: Script not running at the told speed

16 Nov 2019, 21:33

Also SetBatchLines -1 might help here

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: NinjoOnline and 300 guests