feat: add timeout function

timeout(func, time)
works exactly like the `setTimeout` function in javascript
runs `func` after a period of `time` in milliseconds
pull/61/head
sammyette 2021-06-11 19:40:08 -04:00
parent 419e327d95
commit fe3df8c66e
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
1 changed files with 8 additions and 0 deletions

8
lua.go
View File

@ -6,6 +6,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"syscall" "syscall"
"time"
"hilbish/golibs/bait" "hilbish/golibs/bait"
"hilbish/golibs/commander" "hilbish/golibs/commander"
@ -32,6 +33,7 @@ func LuaInit() {
l.SetGlobal("appendPath", l.NewFunction(hshappendPath)) l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("exec", l.NewFunction(hshexec)) l.SetGlobal("exec", l.NewFunction(hshexec))
l.SetGlobal("goro", luar.New(l, hshgoroutine)) l.SetGlobal("goro", luar.New(l, hshgoroutine))
l.SetGlobal("timeout", luar.New(l, hshtimeout))
// yes this is stupid, i know // yes this is stupid, i know
l.PreloadModule("hilbish", HilbishLoader) l.PreloadModule("hilbish", HilbishLoader)
@ -151,3 +153,9 @@ func hshexec(L *lua.LState) int {
func hshgoroutine(gofunc func()) { func hshgoroutine(gofunc func()) {
go gofunc() go gofunc()
} }
func hshtimeout(timeoutfunc func(), ms int) {
timeout := time.Duration(ms) * time.Millisecond
time.AfterFunc(timeout, timeoutfunc)
}