编辑代码

-- 等待指定秒数的协程
function sleep(seconds)
  local co = coroutine.running()
  local timer = os.time() + seconds

  while os.time() < timer do
    coroutine.yield()
  end
end

function Myfunction()
    local startWeight = 100 * math.random()
    while true do
        -- sleep(6)
        local cStamp = os.time()
        local dateTable = os.date("*t", cStamp)
        if dateTable.sec % 5 == 0 then
            print(dateTable)
            local endWeight = startWeight + 100 * math.random()
            local dayProduction = endWeight - startWeight
            startWeight = endWeight
            print("production", dayProduction / 100)
            -- set_var("Production", dayProduction / 100)
            coroutine.wrap(function()
                sleep(1) -- 等待1秒
            end)()
        end
        coroutine.yield()
    end
end

coroutine.wrap(function()
    Myfunction()
end)()