getJobSchedulerEveryNextMillis.lua 894 B

12345678910111213141516171819202122232425262728
  1. local function getJobSchedulerEveryNextMillis(prevMillis, every, now, offset, startDate)
  2. local nextMillis
  3. if not prevMillis then
  4. if startDate then
  5. -- Assuming startDate is passed as milliseconds from JavaScript
  6. nextMillis = tonumber(startDate)
  7. nextMillis = nextMillis > now and nextMillis or now
  8. else
  9. nextMillis = now
  10. end
  11. else
  12. nextMillis = prevMillis + every
  13. -- check if we may have missed some iterations
  14. if nextMillis < now then
  15. nextMillis = math.floor(now / every) * every + every + (offset or 0)
  16. end
  17. end
  18. if not offset or offset == 0 then
  19. local timeSlot = math.floor(nextMillis / every) * every;
  20. offset = nextMillis - timeSlot;
  21. end
  22. -- Return a tuple nextMillis, offset
  23. return math.floor(nextMillis), math.floor(offset)
  24. end