Easiest way to make lua script wait/pause/sleep/block for a few seconds?

前端 未结 19 1360
礼貌的吻别
礼貌的吻别 2020-12-03 00:41

I cant figure out how to get lua to do any common timing tricks, such as

  • sleep - stop all action on thread

  • pause/wait - don\'t go on to the

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 01:39

    It's also easy to use Alien as a libc/msvcrt wrapper:

    > luarocks install alien
    

    Then from lua:

    require 'alien'
    
    if alien.platform == "windows" then
        -- untested!!
        libc = alien.load("msvcrt.dll")
    else
        libc = alien.default
    end 
    
    usleep = libc.usleep
    usleep:types('int', 'uint')
    
    function sleep(ms)
        while ms > 1000 do
            usleep(1000)
            ms = ms - 1000
        end
        usleep(1000 * ms)
    end
    
    print('hello')
    sleep(500)  -- sleep 500 ms
    print('world')
    

    Caveat lector: I haven't tried this on MSWindows; I don't even know if msvcrt has a usleep()

提交回复
热议问题