在 Cocoa 中每分钟运行一次函数
我想知道是否有某种方法可以在 Cocoa 中每分钟运行一个函数。 我个人将使用它来保存用户输入的内容,以防他们退出 - 但我已经看到这种效果也用在 Twitter 客户端中。 当用户保持窗口打开时,它将每 x 分钟自动更新一次,无需用户输入。 似乎这很常见,并且语言允许这样做,我只是似乎找不到有关它的文档。
谢谢你的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
那么你需要自动保存,这需要为你照顾。
Then you want autosaving, which takes care of that for you.
查看 NSTimer 文档。 它完全符合您的要求。 您创建一个可以根据需要重复的 NSTimer,并使用指定的参数调用特定的函数。
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.
更好的解决方案是成为文本视图的委托,并通过创建非重复计时器来响应
textDidChange:
(如果您还没有这样做或者它已经已发射)并将其发射日期设置为未来 X 秒。 然后,用户损失的工作时间不超过 X 秒,最多不超过一分钟,并且当用户没有输入任何内容时,计时器不会触发。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.