go-v8 icon indicating copy to clipboard operation
go-v8 copied to clipboard

Timeouts and Intervals

Open talltyler opened this issue 12 years ago • 0 comments

I would like to implement setTimeout and setInterval. The version that I have created below works but it is blocking anything in the JS from running. I'm new to Go but it seems like the v8 instance needs to be in a go routine and separated from normal execution. Here is my code, am I doing something wrong or is there something else I need to be doing related to the v8 VM to get this working?

js.AddFunc("_set_timeout", func(args ...interface{}) (interface{}, error){
    delay := time.Duration(args[1].(float64)) * time.Millisecond
    ticker := time.NewTicker(delay)
    quit := make(chan struct{})
    go func() {
        for {
            select {
                case <- ticker.C:
                    args[0].(v8.Function).Call()
                case <- quit:
                    ticker.Stop()
                    return
            }
        }
    }()
    time.Sleep(delay)
    close(quit)
    return "", nil
}) 

talltyler avatar Sep 16 '13 17:09 talltyler