在 Cocoa 中每分钟运行一次函数

发布于 2024-07-27 22:20:51 字数 185 浏览 5 评论 0原文

我想知道是否有某种方法可以在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你手 2024-08-03 22:20:52

我个人会将其用于
当用户输入内容时保存内容
如果他们退出了

那么你需要自动保存,这需要为你照顾。

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.

路弥 2024-08-03 22:20:52

查看 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.

樱花细雨 2024-08-03 22:20:51

就我个人而言,我将使用此功能在用户键入内容时保存内容,以防用户退出 - 但我看到这种效果也用在 Twitter 客户端中。

更好的解决方案是成为文本视图的委托,并通过创建非重复计时器来响应 textDidChange:(如果您还没有这样做或者它已经已发射)并将其发射日期设置为未来 X 秒。 然后,用户损失的工作时间不超过 X 秒,最多不超过一分钟,并且当用户没有输入任何内容时,计时器不会触发。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文