Problem with Timer Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1048
Joined: 29 Sep 2013, 22:18

Problem with Timer

22 Jan 2021, 20:50

I have a function that runs a timer and tracks if the user moves away from the window being monitored. Here it is:

Code: Select all

TimeLimit(i)
{
	Global
	if i = 0
	SetTimer, Clock, off
	else
	{
		tim := i * 60000
		;fileappend, %tim%`n, timetest.txt
		setTimer, clock, -%tim%
		Start := A_TickCount
		settimer, stop, 100	
	}
	return
	
	clock:
	settimer, stop, off
	Done()
	Return
	
	stop:
	
	ifWinactive, Ahk_id %Wise%
	return
	else
	{
		settimer, clock, off
		settimer, stop, off
		tim -= A_tickcount - start
		winwaitactive, Ahk_id %Wise%
		fileappend, %tim%`n, timetest.txt
		setTimer, clock, -%tim%
		Start := A_TickCount
		settimer, stop, 100
	}
	return
}
I changed it slightly so that i could have three states rather than two:

Code: Select all

TimeLimit(i)
{
	;soundbeep
	Start := A_TickCount
	
	if !i
	SetTimer, Clock, off
	
	tim := !i ? 0 : i * 1000

	if tim != 0
	{
		setTimer, clock, -%tim%		
		settimer, stop, 100	
	}
	return
	
	clock:
	settimer, stop, off
	Done()
	Return
	
	stop:
	
	ifWinactive, Ahk_id %Wise%
	return
	else
	{
		settimer, clock, off
		settimer, stop, off
		tim -= A_tickcount - start
		winwaitactive, Ahk_id %Wise%
		fileappend, %tim%`n, timetest.txt
		setTimer, clock, -%tim%
		Start := A_TickCount
		settimer, stop, 100
	}
	return
}
But now it doesn't work at all. For some reason the stop timer fires immediately and stops the clock timer as soon as it starts. I can't figure out what I've done wrong. The only thing that I've changed is the function not the window being monitored. Can anyone explain my mistake. Thanks.
User avatar
mikeyww
Posts: 5368
Joined: 09 Sep 2014, 18:38

Re: Problem with Timer

22 Jan 2021, 21:00

Have you examined the value of Wise in the new function?
User avatar
PuzzledGreatly
Posts: 1048
Joined: 29 Sep 2013, 22:18

Re: Problem with Timer

22 Jan 2021, 21:48

mikeyww wrote:
22 Jan 2021, 21:00
Have you examined the value of Wise in the new function?
Thanks for the reply. It refers to the same window. Nothing has changed in the script apart from the timer function.
User avatar
mikeyww
Posts: 5368
Joined: 09 Sep 2014, 18:38

Re: Problem with Timer

22 Jan 2021, 21:56

When you add a line to display the value of Wise inside the function, please tell us what value is reported.
User avatar
PuzzledGreatly
Posts: 1048
Joined: 29 Sep 2013, 22:18

Re: Problem with Timer

22 Jan 2021, 22:12

I should have mentioned I already tried adding two message boxes, one at the top of the function and one in the stop timer. They both gave the same value, the title of the window being monitored.
User avatar
boiler
Posts: 6873
Joined: 21 Dec 2014, 02:44

Re: Problem with Timer  Topic is solved

23 Jan 2021, 05:49

You removed Global from the function, so unless you’ve made the variable Wise a global variable somewhere else in your script, the function wouldn’t know its value. Are you sure you checked its value in the new function?
User avatar
mikeyww
Posts: 5368
Joined: 09 Sep 2014, 18:38

Re: Problem with Timer

23 Jan 2021, 06:18

You can also check the value of the parameter that you are passing, by showing it inside the function. What value does the function report for i?

What is "Done()"?

Please post your revised script.
User avatar
PuzzledGreatly
Posts: 1048
Joined: 29 Sep 2013, 22:18

Re: Problem with Timer

24 Jan 2021, 06:31

boiler wrote:
23 Jan 2021, 05:49
You removed Global from the function, so unless you’ve made the variable Wise a global variable somewhere else in your script, the function wouldn’t know its value. Are you sure you checked its value in the new function?
Thanks boiler, the missing global was the problem. I did check using the correct script but the issue wasn't the window name but as you pointed out that the timer wasn't seeing the name. When I checked for the name inside the stop time with my msgbox I was checking which window was active and of course it was the same. Thank you both for your help. I'm trying to cut down on declaring everything as global inside my functions butstill have lots to learn.

Return to “Ask For Help”

Who is online

Users browsing this forum: boiler, braunbaer, malcev, mikeyww, neo256, newbieforever, swub and 47 guests