9

[Golang/GopherJS] setTimeout

 3 years ago
source link: http://siongui.github.io/2016/06/03/go-gopherjs-setTimeout/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

[Golang/GopherJS] setTimeout

June 03, 2016

JavaScript setTimeout equivalent in Go/GopherJS - time.AfterFunc.

Demo

Source code: time.AfterFunc is Go's setTimeout.

index.html | repository | view raw

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>GopherJS setTimeout Demo</title>
</head>
<body>

<button id="btn" type="button">Click Me!</button>
<br><br>
<div id="info">Click button to start 3 second timer</div>

<script src="app.js"></script>
</body>
</html>

app.go | repository | view raw

package main

import (
	"github.com/gopherjs/gopherjs/js"
	"time"
)

func main() {
	info := js.Global.Get("document").Call("getElementById", "info")
	button := js.Global.Get("document").Call("getElementById", "btn")

	button.Set("onclick", func(e *js.Object) {
		// time.AfterFunc is Go's setTimeout
		time.AfterFunc(3*time.Second, func() {
			info.Set("innerHTML", "time is up")
		})
	})
}

References:

[2]GitHub - siongui/gopherjs-tooltip: Tooltip in Go. Compiled to JavaScript via GopherJS

[4]Are multiple time.After statements racing in "select" : golang


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK