【发布时间】:2017-10-25 03:49:13
【问题描述】:
我在下面有两个函数,我需要按顺序执行它们,只有当第一个函数完成下一次运行时。但是,仅输出第二个函数不会发生这种情况。
在原始 Lua 中,我们可以使用 CALLBACK_MANAGER:FireCallbacks 和 CALLBACK_MANAGER:RegisterCallback 来处理这个问题。我该如何继续使用 esp8266?
-- test.lua
function increase()
a = 0
tmr.alarm(1,1000,1,function()
if (a == 10) then
tmr.stop(1)
else
a = a + 1
end
print(a)
end)
end
function decrease()
a = 10
tmr.alarm(1,1000,1,function()
if (a == 0) then
tmr.stop(1)
else
a = a - 1
end
print(a)
end)
end
function start()
increase()
decrease()
end
start()
输出
➜ test git:(master) ✗ nu exec test.lua
➜ test git:(master) ✗ nu terminal
-- output
--- Miniterm on /dev/cu.wchusbserial1410 115200,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---
10
9
8
7
6
5
4
3
2
1
有什么想法吗?提前致谢!
【问题讨论】:
-
当所有增加的工作完成后,你应该从
increase()内部调用decrease()。 -
这是一个好方法吗?如果有一段时间我只需要执行
increase()怎么办?