Simple timer with current date

Was playing with the current date… and made a timer of it :wink:

set theTimer to (current date) + (minutes * 1) + (30) -- 1 minutes + 30 seconds

repeat while theTimer is not (current date)
	if theTimer is (current date) then
		display alert "Time stopped..."
		exit repeat
	end if
end repeat

Kind of fun. I usually use a timer on my phone or ipad when doing something like letting tea steep. This might be more convenient when I’m at my desk.

I couldn’t help but wonder about how many times that repeat loop ran though so I added a counter and while it varied, it was typically around 130K cycles per minute (on my old, slow mac). I should note that a kernel_task is running in the background and chewing up most of the remaining CPU so these numbers might look different were my machine otherwise idle (and probably be lower).

I then ran it with Activity Monitor open to see if it had an impact on CPU usage. When run from script editor, it typically ran at about 85% CPU. Yikes!

I then exported the script to its own app and ran that. It was more efficient but also ran more loops. It typically ran at 40% CPU and 180K cycles per minute.

To reduce the work my mac has to do, I added a delay to the script and set it to 90% of the timer’s duration… so the cycling won’t begin until the timer reaches the home stretch.

set x to 0
set tDur to (minutes * 0) + (60) -- 60 seconds
set theTimer to (current date) + tDur
set beginCheck to tDur * 0.9

delay beginCheck
repeat while theTimer is not (current date)
	set x to x + 1
	if theTimer is (current date) then
		tell me to activate
		display alert "Time stopped 1… after " & x & " loops"
		exit repeat
	end if
end repeat

As an app, (after a lurch at launch) it runs at .2% while idling and then spikes to between 20-40% CPU. After a sixty second run, it cycled about 40K times (in the final 6 seconds). I might just set it to delay until the last three seconds and leave it at that.

Hi Fredrik.

The way you’ve written it, the alert will only be displayed if the timer happens to tick over to the end time in the instant between the test at the top of the repeat and the test governing the alert itself. Otherwise the script will just stop when the time’s reached. This is more reliable:

set theTimer to (current date) + (minutes * 1) + (30) -- 1 minutes + 30 seconds

repeat until theTimer is (current date)
end repeat
display alert "Time stopped..."

Interesting Nigels code use 150% cpu usage on my m1 Apple Silicon in Script Editor

But this code only make 1% in Script Editor

set x to 0
set tDur to (minutes * 0) + (60) -- 60 seconds
set theTimer to (current date) + tDur
set beginCheck to tDur * 0.9

delay beginCheck
repeat until theTimer is (current date)
    set x to x + 1
end repeat
display alert "Time stopped 1… after " & x & " loops"

By remove the delay the cpu usage raise… but this one only give 100%

set x to 0
set theSeconds to 60
set theTimer to (current date) + theSeconds
repeat until theTimer is (current date)
    set x to x + 1
end repeat
display alert "Time stopped 1… after " & x & " loops"

This had me scratching my head. That’s why my ‘alert’ has the ‘1’ in it… because I stuck a second alert at the very end of the script to see if I could get an alert to run at all in the script editor and I wanted to know which actually ran. Randomly, the alert came up normally most times that I ran it from an application.

Thanks for clarifying the issue.

Model: Late 2013 macbook pro, Sierra
Browser: Firefox 108.0
Operating System: macOS 12

I did some research on the net and got one idea…
If there is a sleep (delay) in the repeat loop the cpu usage will be lower.

This code only make 30% cpu usage for Script Editor

set theSeconds to 30
set theTimer to (current date) + theSeconds

repeat until theTimer is (current date)
	delay 0.01
end repeat
display alert "Time stopped..."

On other hand… if we delay with 1 millisecond we will get 30000 / 2 loops.

set x to 0
set theSeconds to 30
set theTimer to (current date) + theSeconds

repeat until theTimer is (current date)
	delay 1 / 1000
	set x to x + 1
end repeat
display alert "Time stopped... " & "count steps: " & x