在Cocoa中运行每分钟的函数

时间:2023-01-02 01:08:53

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.

我想知道是否有某种方法可以在Cocoa中每分钟运行一个函数。我个人将使用它来保存内容,因为用户在退出时键入内容 - 但我已经看到这种效果也在Twitter客户端中使用。当用户保持窗口打开时,它将在没有用户输入的情况下每x分钟自动更新。好像这很常见,语言允许它,我似乎无法找到它的文档。

Thanks for any help!

谢谢你的帮助!

3 个解决方案

#1


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.

我个人将使用它来保存内容,因为用户在退出时键入内容 - 但我已经看到这种效果也在Twitter客户端中使用。

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.

更好的解决方案是成为文本视图的委托,并响应textDidChange:通过创建非重复计时器(如果尚未执行此操作或已经触发)并将其触发日期设置为X秒。然后,用户失去的工作时间不超过X秒,而不是一分钟,并且当用户没有输入任何内容时,计时器不会触发。

#2


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.

然后你想要自动保存,它会照顾你。

#3


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.

查看NSTimer文档。它完全符合你的要求。您创建一个NSTimer,它将根据需要重复,并使用指定的参数调用特定的函数。

#1


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.

我个人将使用它来保存内容,因为用户在退出时键入内容 - 但我已经看到这种效果也在Twitter客户端中使用。

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.

更好的解决方案是成为文本视图的委托,并响应textDidChange:通过创建非重复计时器(如果尚未执行此操作或已经触发)并将其触发日期设置为X秒。然后,用户失去的工作时间不超过X秒,而不是一分钟,并且当用户没有输入任何内容时,计时器不会触发。

#2


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.

然后你想要自动保存,它会照顾你。

#3


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.

查看NSTimer文档。它完全符合你的要求。您创建一个NSTimer,它将根据需要重复,并使用指定的参数调用特定的函数。