From fe3df8c66ecf380afa0e5527fb8709a811ba5a10 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Fri, 11 Jun 2021 19:40:08 -0400 Subject: [PATCH] feat: add timeout function timeout(func, time) works exactly like the `setTimeout` function in javascript runs `func` after a period of `time` in milliseconds --- lua.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lua.go b/lua.go index 0827226..66f6fd5 100644 --- a/lua.go +++ b/lua.go @@ -6,6 +6,7 @@ import ( "os/exec" "strings" "syscall" + "time" "hilbish/golibs/bait" "hilbish/golibs/commander" @@ -32,6 +33,7 @@ func LuaInit() { l.SetGlobal("appendPath", l.NewFunction(hshappendPath)) l.SetGlobal("exec", l.NewFunction(hshexec)) l.SetGlobal("goro", luar.New(l, hshgoroutine)) + l.SetGlobal("timeout", luar.New(l, hshtimeout)) // yes this is stupid, i know l.PreloadModule("hilbish", HilbishLoader) @@ -151,3 +153,9 @@ func hshexec(L *lua.LState) int { func hshgoroutine(gofunc func()) { go gofunc() } + +func hshtimeout(timeoutfunc func(), ms int) { + timeout := time.Duration(ms) * time.Millisecond + time.AfterFunc(timeout, timeoutfunc) +} +