Curiosity: strange behavior repeat-loop

Hi all,

I have been struggling for two nights with a strange problem caused by a repeat-loop in an AppleScript project. At the end I managed to risolve it but more casually than consciously and maybe someone has a kind of “scientific” explanation why the following example 1 won’t work and example 2 does :smiley: .

I have a simple timer placed inside a drawer which should be stopped - together with the rest of the script activity - when a “Stop button” inside the same drawer is clicked.
This example doesn’t work: the timer goes on infinitely because all the controls of the interface are enabled but won’t receive any mouseclick, the only way to get out of the loop is forcequitting the application:

property counterTime : "00:00:00" -- global because I need it elsewhere in the script
property d : date counterTime -- idem
property stopTimer : false -- should be set to true by the "Stop Button" to stop the loop

-- (...) a lot of other stuff that works fine

on displayCounter()
	repeat
		if stopTimer = false then
			set d to d + 1
			set contents of text field "counter" of drawer "controlli" of window "main" to d
			delay 1
		else
			exit repeat
		end if
	end repeat
end displayCounter

Playing around with the code I casually modified the script in the following way and now everything works perfectly, the only thing is I don’t know why :stuck_out_tongue:

property counterTime : "00:00:00"
property stopTimer : false

on awakeFromNib theObject
set d to date counterTime
end awakeFromNib

on displayCounter()
	repeat
		if stopTimer = false then
			set d to d + 1
			set contents of text field "counter" of drawer "controlli" of window "main" to d
			delay 1
		else
			exit repeat
		end if
	end repeat
end displayCounter

As you see the only difference is that now the variable d is initiated inside the “awakeFromNib” handler. Now, however, with the repeat loop running all the controls will do what they are expected to do.
Why ? :rolleyes:

Thanks for any suggestion & good scripting
Farid