Lua中的定时脚本/功能 [英] Timed script/function in Lua
问题描述
早上好朋友...我正在为移动应用程序使用Lua脚本语言,并且有如下要求-
good morning friends... I am working on with Lua scripting language for a mobile app and have a requirement as follows -
该应用程序的主要目的是为有医生的个人安排约会. 因此,一旦安排好用户的约会,例如5月8日下午4:30,用户应该在一个小时前(即下午3:30)收到提醒警报".
The application's main aim is to schedule appointments for an individual with a Doctor. So once a user's appointment is scheduled, for e.g. 8th May @ 4:30 PM, the user should receive a "reminder alert" before an hour i.e. @ 3:30 PM.
我对如何完成这项工作绝对一无所知. 我可以获取用户的日期时间值,并使用 函数 应该在该日期时间的60分钟之前调用的逻辑.该功能包含我的警报消息". 但是该怎么做呢?
am absolutely having a blank mind on how to get this done. I can get the user's date-time value and use the logic that a function should invoke just before 60 mins of that date-time. And that function contains my "Alert message". But how to do this?
有人可以给我一个线索吗?
Can anyone guide me with a clue?
请让我知道是否需要其他输入...
Please let me know if any other inputs are required...
谢谢.
推荐答案
我会采用这样的方法:
1.
将每个约会的详细信息存储为包含JSON或Lua表格数据的.txt文件,如下所示:
Store each appointment's details as a .txt file containing JSON or Lua tabular data something like this:
{
date = "14:30 01/07/2013";
dateBooked = "09:30 23/06/2013";
venue = "31 Dentist Street";
appointmentType = "Routine Teeth Cleaning";
}
2.
您可以像这样设置一个计时器类
You can have a timer class like so
Timer = {}
Timer_mt = { __index = Timer; __add = function(a,b) a:tickBy(b) end ; }
function Timer:new(delayTime,callBack)
local timer = {callBack=callBack}
timer.initTime = os.date() --MM/DD/YY HH:MM:SS
--delayTime = HH:MM:SS
_,_,hour,minute,second = string.find(delayTime,"(%d%d):(%d%d):(%d%d)")
timer.delay = {hour=hour,minute=minute,second=second}
--time the timer started
_,_,hour,minute,second = string.find(timer.initTime,"(%d%d):(%d%d):(%d%d)")
timer.startTime = {hour=hour,minute=minute,second=second}
--time the timer started
timer.initTime = os.date() --MM/DD/YY HH:MM:SS
print(timer.initTime)
_,_,hour,minute,second = string.find(timer.initTime,"(%d%d):(%d%d):(%d%d)")
timer.currentTime = {hour=hour,minute=minute,second=second}
return setmetatable(timer,Timer_mt)
end
function Timer:tick() --returns true if time expired
currTime = os.date() --MM/DD/YY HH:MM:SS
_,_,chour,cminute,csecond = string.find(currTime,"(%d%d):(%d%d):(%d%d)")
if chour - self.startTime.hour >= tonumber(self.delay.hour) and cminute - self.startTime.minute >= tonumber(self.delay.minute) and csecond - self.startTime.second > tonumber(self.delay.second) then
self:callBack()
self.startTime.hour,self.startTime.minute, self.startTime.second = chour,cminute,csecond
--return true
end
--return false
end
t = Timer:new("00:00:02",function () print("DONE") end)
print(t.currentTime.hour,t.currentTime.minute,t.currentTime.second)
while t:tick() or true do
io.read()
end
(我刚刚整理好了,所以我建议您对其进行测试,但它似乎对我有用).
(I just made this up so I advice you test it but it seems to work for me).
3.
在启动时或添加新约会时,创建一个新计时器,然后在主执行过程中的某个时间点每个tick()
,您甚至可以拥有一个计时器,它是您唯一的tick()
计时器,它是回调的ticks()
其他...无论如何设置每个计时器的回调以显示警报
3.
On start-up, or when a new appointment is added create a new timer, and then tick()
each one at some point during the main execution, you could even have a timer which is the only one you tick()
and it's callback ticks()
the others... Anyway set the callback for each timer to display an alarm
这篇关于Lua中的定时脚本/功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!