从 Lua 脚本触发时睡眠命令不起作用

问题描述 投票:0回答:2
function sleep(n)
    os.execute("sleep " .. tonumber(n))
  end

sleep(2)
print("hi")

这是我的代码,终端返回这个,

“睡眠”不被识别为内部或外部命令、可操作程序或批处理文件。

lua
2个回答
-1
投票

使自己的系统独立

sleep()
更像...

function sleep(sec)
local se = os.time() + sec
while os.time() < se do
  io.write('\b','-')
  io.write('\b','\\')
  io.write('\b','|')
  io.write('\b','/')
  io.write('\b','-')
end
io.write('\b\n')
end

...但是Lua会循环afap,包括预热CPU和最大风扇速度。

也许等待用户接受会更好?

function pause(text)
local text = text or 'Press Enter '
io.write(text)
io.read()
end

这不会花费任何 CPU 时间。


-1
投票

尝试这个代码:

function sleep (s) 
    local sec = tonumber(os.clock() + s); 
    while (os.clock()<sec) do 
    end 
end
sleep(2)
print("hi")

您可以在 Windows 上使用此代码:

function sleep(n)
    os.execute('powershell "Sleep ' .. tonumber(n) .. '"' )
end

或使用 ping:

function sleep(n)
  os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") 
end
© www.soinside.com 2019 - 2024. All rights reserved.