Skip to content

Add a Timer to the Lua API to suspend interactive scripts #3251

Description

@API-Beast

Right now the only way to wait for some time in a lua script is to do a busy wait:

function sleep(a) 
    local sec = tonumber(os.clock() + a); 
    while(os.clock() < sec) do 
    end 
end

However this will lock up the entire program until the sleep function has returned.

If a Timer class or similar would be added, or simply a callback that is called every n seconds it would open up some new ways scripts can interact with the user. In my case I was trying to implement a timeout to WebSocket so that if it hasn't managed to connect yet after 3 seconds it would stop trying to connect and update the UI to allow the user to manually restart the connection process.

API could look something like this:

local timer = Timer{delay=1.0, onTick=function() print("Tick Tock") end, repeat=true}
timer:start()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureFeature request, or something should be improvedscriptingRelated to scripting APItime-3

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions