如何防止这个Lua脚本被反复发送?

时间:2016-11-09 14:38:31

标签: button io lua command

我有一个物理按钮,我在现实生活中按下这个Lua脚本。它工作得很好,但是,如果按下并按住按钮,它会一遍又一遍地发送脚本直到我松开按钮。我该怎么做才能防止这种情况发生?

commandSent = 0
enableDebug()

while true do

--if input turns on and command has not been sent, send command
if io.input1 == 1 and commandSent == 0 then
httpRequest("http://xxx.xxx.xx.xx/axis-cgi/virtualinput/activate.cgi?schemaversion=1&port=1",50)
 print("input turned on")
 sleep(50)
httpRequest("http://xxx.xxx.xx.xx/axis-cgi/virtualinput/deactivate.cgi?schemaversion=1&port=1",50)
 commandSent = 1
end

--when input turns off, reset commandSent flag to 0
if io.input1 == 0 then
 print("input turned off")
commandSent = 0
end 
end

1 个答案:

答案 0 :(得分:1)

我修好了。添加" local"到脚本工作=)见下文:

local commandSent = 0
enableDebug()

while true do

--if input turns on and command has not been sent, send command
if io.input1 == 1 and commandSent == 0 then
httpRequest("http://xxx.xxx.xx.xx/axis-cgi/virtualinput/activate.cgi?schemaversion=1&port=1",50)
 print("input turned on")
 sleep(50)
httpRequest("http://xxx.xxx.xx.xx/axis-cgi/virtualinput/deactivate.cgi?schemaversion=1&port=1",50)
 commandSent = 1
end

--when input turns off, reset commandSent flag to 0
if io.input1 == 0 then
 print("input turned off")
commandSent = 0
end 
end