Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. local Jobs = {}
  2. local LastTime = nil
  3.  
  4. function RunAt(h, m, cb)
  5.  
  6. table.insert(Jobs, {
  7. h = h,
  8. m = m,
  9. cb = cb
  10. })
  11.  
  12. end
  13.  
  14. function GetTime()
  15.  
  16. local timestamp = os.time()
  17. local d = os.date('*t', timestamp).wday
  18. local h = tonumber(os.date('%H', timestamp))
  19. local m = tonumber(os.date('%M', timestamp))
  20.  
  21. return {d = d, h = h, m = m}
  22.  
  23. end
  24.  
  25. function OnTime(d, h, m)
  26.  
  27. for i=1, #Jobs, 1 do
  28. if Jobs[i].h == h and Jobs[i].m == m then
  29. Jobs[i].cb(d, h, m)
  30. end
  31. end
  32.  
  33. end
  34.  
  35. function Tick()
  36.  
  37. local time = GetTime()
  38.  
  39. if time.h ~= LastTime.h or time.m ~= LastTime.m then
  40. OnTime(time.d, time.h, time.m)
  41. LastTime = time
  42. end
  43.  
  44. SetTimeout(1000, Tick)
  45. end
  46.  
  47. LastTime = GetTime()
  48.  
  49. Tick()
  50.  
  51. AddEventHandler('cron:runAt', function(h, m, cb)
  52. RunAt(h, m, cb)
  53. end)
  54.  
  55. print('CRON STARTED [' .. os.date('%H:%M', os.time()) .. ']')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement