"Cancel" button to exit a repeat?

I made an app that runs a repeating script when a button is clicked (the script is manipulating text and using system events to paste parts of the text into other windows and applications).

I need to make a button that will cancel the aforementioned script.

I made a button and used:

on clicked theObject
if the name of theObject is "startButton" then
--run the repeating script
else if the name of theObject is "cancelButton" then
exit repeat
end if
end clicked

I tried this after doing a search for this topic earlier and finding the “exit repeat” bit.

How can I make this work?

Thanks!

try something like this:


property doScript : false

-- in idle handler you have your loop
on idle
        -- this might keep you from clicking
	repeat until not doScript
		-- your loop
	end repeat
	
	-- so this should do the trick
	if doScript then
		-- your script
	end if
end idle

-- the clicked handler sets doScript's value
on clicked theObject
	if the name of theObject is "startButton" then
		-- run the repeating script by:
		set doScript to true
	else if the name of theObject is "cancelButton" then
		set doScript to false
	end if
end clicked