遵循有关https://nodemcu.readthedocs.io/en/release/modules/tmr/#tobjcreate的说明
我正在尝试在我的Lua脚本中创建一个计时器,它将每10秒执行一个函数。我的脚本中的示例计时器:
mytimer = tmr.create
mytimer:register(10000, tmr.ALARM_AUTO, my_function() end)
mytimer:start()
当我执行我的脚本时,我得到一个语法错误:
'=' expected near 'mytimer'
我在这里做错了什么?
谢谢
发布于 2021-10-14 16:09:53
与其在这里提问,不如将您的代码与您链接的文档中的代码示例进行比较:
local mytimer = tmr.create()
mytimer:register(5000, tmr.ALARM_SINGLE, function (t) print("expired"); t:unregister() end)
mytimer:start()
示例:local mytimer = tmr.create()
You mytimer = tmr.create
你找不到接线员了。不将mytimer
设为本地化是一种糟糕的做法,但不会给您带来错误。
示例:mytimer:register(5000, tmr.ALARM_SINGLE, function (t) print("expired"); t:unregister() end)
你:mytimer:register(10000, tmr.ALARM_AUTO, my_function() end)
我不知道my_function
是什么。结尾不属于这里,除非您在适当的地方定义了一个函数。那么它应该与示例中所示类似。只有在没有end
并且my_function()
返回函数值的情况下,您的版本才是正确的。
function (t) print("expired"); t:unregister() end
定义了一个匿名函数。它解析为一个函数值,该函数值用作注册函数的回调参数的实参。
你也可以像这样做:
local callback = function (t) print("expired"); t:unregister() end
mytimer:register(5000, tmr.ALARM_SINGLE, callback)
任何其他内容在手册中都有描述。
https://stackoverflow.com/questions/69573780
复制相似问题