如何从 NSTimer 中找到剩余的时间间隔

编程入门 行业动态 更新时间:2024-10-11 17:24:31
本文介绍了如何从 NSTimer 中找到剩余的时间间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我设置了一个 NSTimer.scheduledTimerWithTimeInterval 方法,该方法每 20 分钟间隔一次.我希望能够找出应用程序进入后台模式时还剩下多少时间.我如何知道间隔还剩多少时间?

I have set a NSTimer.scheduledTimerWithTimeInterval method which has an interval every 20 minutes. I want to be able to find out how much time is left when the the app goes into background mode. How do I find out how much time is left from the interval?

谢谢

推荐答案

您可以访问 NSTimer 的 fireDate,它会告诉您计时器何时将再次触发.

You have access to a NSTimer's fireDate, which tells you when the timer is going to fire again.

现在和 fireDate 之间的区别是您可以使用 NSDate 的 timeIntervalSinceDate API.

The difference between now and the fireDate is an interval you can calculate using NSDate's timeIntervalSinceDate API.

E.G.类似:

let fireDate = yourTimer.fireDate let nowDate = NSDate() let remainingTimeInterval = nowDate.timeIntervalSinceDate(fireDate)

更多推荐

如何从 NSTimer 中找到剩余的时间间隔

本文发布于:2023-11-30 00:02:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:间隔   剩余   时间   中找到   NSTimer

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!