12 Apr Как в Lua реализовать функцию задержки выполнения скрипта.
Posted at 16:22h
in
Добрый день!
Как в Lua реализовать функцию задержки типа delay в СИ?
1 answer
Добрый день
Задержку можно реализовать с применением встроенной функции GetTime()
http://docs.webhmi.com.ua/misc_functions?s%5B%5D=gettime
для определения времени с точностью до наносекунд.
function main (userId)
local start_time = GetTime();
wait_ms(100.25, GetTimePrecise())
-- Uncomment this part to measure the time spent
-- local cur_time = GetTime()
-- cur_sec = cur_time.sec
-- cur_ns = cur_time.ns
-- local time_spent_sec = cur_sec - start_time.sec
-- local time_spent_ns = cur_ns - start_time.ns
-- if time_spent_sec == 0 then
-- real_spent = time_spent_ns / 1000000
-- elseif time_spent_ns ~= 1 then
-- real_spent = time_spent_sec * 1000 + time_spent_ns / 1000000
-- end
-- ERROR("in fact read spent ms = " .. real_spent)
--
end
function GetTimePrecise()
local start_time = GetTime();
return start_time.sec * 1000^3 + start_time.ns
end
function wait_ms (milliseconds, precise_start_time)
repeat
delay_ns = milliseconds * 1000000
local cur_time = GetTime();
time_over = GetTimePrecise() > (precise_start_time + delay_ns)
until time_over
end