• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

kikito/cron.lua: Time-related functions for Lua, inspired in javascript's se ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

kikito/cron.lua

开源软件地址(OpenSource Url):

https://github.com/kikito/cron.lua

开源编程语言(OpenSource Language):

Lua 100.0%

开源软件介绍(OpenSource Introduction):

cron.lua

Build Status

cron.lua are a set of functions for executing actions at a certain time interval.

API

local clock = cron.after(time, callback, ...). Creates a clock that will execute callback after time passes. If additional params were provided, they are passed to callback.

local clock = cron.every(time, callback, ...). Creates a clock that will execute callback every time, periodically. Additional parameters are passed to the callback too.

Clock methods:

local expired = clock:update(dt). Increases the internal timer in the clock by dt.

  • On one-time clocks, if the internal timer surpasses the clock's time, then the clock's callback is invoked.
  • On periodic clocks, the callback is executed 0 or more times, depending on how big dt is and the clock's internal timer.
  • expired will be true for one-time clocks whose time has passed, so their function has been invoked.

clock:reset([running]) Changes the internal timer manually to running, or to 0 if nothing is specified. It never invokes callback.

Examples

local cron = require 'cron'

local function printMessage()
  print('Hello')
end

-- the following calls are equivalent:
local c1 = cron.after(5, printMessage)
local c2 = cron.after(5, print, 'Hello')

c1:update(2) -- will print nothing, the action is not done yet
c1:update(5) -- will print 'Hello' once

c1:reset() -- reset the counter to 0

-- prints 'hey' 5 times and then prints 'hello'
while not c1:update(1) do
  print('hey')
end

-- Create a periodical clock:
local c3 = cron.every(10, printMessage)

c3:update(5) -- nothing (total time: 5)
c3:update(4) -- nothing (total time: 9)
c3:update(12) -- prints 'Hello' twice (total time is now 21)

Gotchas / Warnings

  • cron.lua does not implement any hardware or software clock; you will have to provide it with the access to the hardware timers, in the form of periodic calls to cron.update
  • cron does not have any defined time units (seconds, milliseconds, etc). You define the units it uses by passing it a dt on cron.update. If dt is in seconds, then cron will work in seconds. If dt is in milliseconds, then cron will work in milliseconds.

Installation

Just copy the cron.lua file somewhere in your projects (maybe inside a /lib/ folder) and require it accordingly.

Remember to store the value returned by require somewhere! (I suggest a local variable named cron)

local cron = require 'cron'

Also, make sure to read the license file; the text of that license file must appear somewhere in your projects' files.

Specs

This project uses busted for its specs. If you want to run the specs, you will have to install it first. Then run:

cd path/where/the/spec/folder/is
busted



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
juce/lua-resty-shell: tiny subprocess/shell library to use with OpenResty applic ...发布时间:2022-08-16
下一篇:
weshoke/Lust: Lua String Templates发布时间:2022-08-16
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap