如何使用 NSDate 制作计数器?
我想开发一个餐厅系统,它想要计算订单剩余时间(比如披萨订单剩余 5 薄荷糖),
我想知道这个计数器的基本代码是什么,比如我如何使用 NSDate 或任何其他类???
请指导
I want to develop a system for restaurant, which want to calculate time remaining in order(like 5 mints remaining in order of pizza),
I want to know what basic code be for this counter, like how can I use NSDate or any other class???
Please guide
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获取自某个 NSDate 以来的秒数:
[NSDate date] 返回当前 NSDate 对象,并且等于当前日期和时间。
Get amount of seconds since a certain NSDate:
[NSDate date] returns the current NSDate object, and thats equal to the current date and time.
我认为您可以实现一个循环计时器,每 30 秒一次(不要太快),它将轮询所有待处理的请求并更新每个订单的倒计时,并向您发出所有达到倒计时的订单的信号。
例如:假设您点了 5 分钟的订单。然后,您将通过在当前日期上添加 5*60 秒来计算订单准备就绪的日期:
然后,您将该日期存储到订单结束日期属性中。例如:
在应用程序启动时,您将定义一个循环计时器,该计时器每 30 秒触发一次,并调用“countdownCheck”函数:
此时,您在 countdownCheck 中要做的就是循环遍历您的 Order 实例,并为每个实例计算剩余时间:
显然,这种方法对时间要求不高,唯一的要求是在 30 秒限制内运行 countdownCheck 内的代码,这当然绰绰有余。理想情况下,只要循环足够快,您就可以将其设置为 1 秒。
I think you can implement a recurring timer, paced each 30 seconds (don't do it too fast), that will poll all pending requests and update the countdown for each order and will signal you all orders that reached the countdown.
E.g.: let's say you take a 5 minutes order. Then you will calculate the date the order will be ready by adding 5*60 seconds to the current date:
Then you will store this date to your order end date property. E.g.:
At application startup you will define a recurring timer that will trigger each 30 seconds and will call the "countdownCheck" function:
At this point what you have to do in your countdownCheck is to loop through your Order instances and for each of them calculate the time remaining:
Clearly this approach is not time critical, the only requirement you have is to run the code inside the countdownCheck in the 30 seconds limit, which is of course more than enough. You can ideally set it to 1 second only, provided your loop is fast enough.