2.4-Air302(NB-IOT)-基础外设-延时,定时器

时间:2022-07-28
本文章向大家介绍2.4-Air302(NB-IOT)-基础外设-延时,定时器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

<p><iframe name="ifd" src="https://mnifdv.cn/resource/cnblogs/Learn-NB-IOT-Air302-ForLua" frameborder="0" scrolling="auto" width="100%" height="1500"></iframe></p>

 延时

sys.wait(5000) --延时5S

延时函数一般配合任务使用

sys.taskInit(function()
    while 1 do
        sys.wait(500) --延时500ms
        log.info("111111111111111111111111")
    end
end)

一次性定时器

-- 一次性定时器
function OneTimer(param)
    print(param);
end

--一次性定时器: sys.timerStart
--OneTimer:回调函数
--5000:延时5S
--最后一个参数:传给回调函数的参数
sys.timerStart(OneTimer,5000,"delay 5s")

 程序启动延时5S后打印

循环定时器

-- 循环定时器
local cnt = 0;
function LoopTimer(param)
    print(param)
    cnt = cnt+1
    if(cnt>=6) then
        cnt = 0;
        sys.timerStop(timerLoop) --停止循环定时器
        print("stop timerLoop")
    end
end

--循环定时器: sys.timerLoopStart
--LoopTimer:回调函数
--1000:延时1S
--最后一个参数:传给回调函数的参数
--timerLoop:返回的参数用于关闭定时器
timerLoop = sys.timerLoopStart(LoopTimer,1000,"this timerLoop")