individualtermite
individualtermite

Reputation: 3755

Run Function Every Minute In Cocoa

I am wondering if there is some way to run a function every minute in Cocoa. I, personally, will be using this for saving content as the user types it in case they quit - But I have seen this effect also used in Twitter clients. When the user keeps the window open, it will auto-update every x minutes without input from the user. It seems as if this is common, and the language allows it, I just can't seem to find documentation on it.

Thanks for any help!

Upvotes: 2

Views: 628

Answers (3)

Dan Lorenc
Dan Lorenc

Reputation: 5394

Check out the NSTimer docs. It does exactly what you want. You create an NSTimer that will repeat for as long as you want, and call a specific function with specified arguments.

Upvotes: 2

user23743
user23743

Reputation:

I, personally, will be using this for saving content as the user types it in case they quit

Then you want autosaving, which takes care of that for you.

Upvotes: 2

Peter Hosey
Peter Hosey

Reputation: 96323

I, personally, will be using this for saving content as the user types it in case they quit - But I have seen this effect also used in Twitter clients.

A better solution would be to be the text view's delegate, and respond to textDidChange: by creating the non-repeating timer (if you have not done so already or it has already fired) and setting its fire date to X seconds in the future. Then, the user loses no more than X seconds' worth of work, not up to one minute, and the timer is not firing when the user has not typed anything.

Upvotes: 6

Related Questions