ticker

package module
v0.0.0-...-cf4e81c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 7, 2014 License: MIT Imports: 1 Imported by: 0

README

ticker

GoDoc

Schedule the repeated execution of a function every time.Duration.

View the docs.

Installation

$ go get github.com/r8k/ticker

Example

package main

import "fmt"
import "time"
import "github.com/r8k/ticker"

func print(args []interface{}) {
    fmt.Printf("Hello World !! Got %d args\n", len(args))
    for i, s := range args {
        fmt.Println(i, s)
    }
}

func main() {
    stop := ticker.TickEvery(print, time.Second*3, []interface{}{"a", 2})
    time.Sleep(time.Second * 10) // sleep for 10 seconds, to see 3 print invocations
    stop <- true                 // send a stop signal
    time.Sleep(time.Second * 2)  // sleep for 2 more seconds, to ensure, it stopped :)
}

Run the above example

go run main.go

And you would see:

❯ go run main.go 
Hello World !! I got 2 args
0 a
1 2
Hello World !! I got 2 args
0 a
1 2
Hello World !! I got 2 args
0 a
1 2

License

MIT

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func TickEvery

func TickEvery(fn func(args []interface{}), d time.Duration, args []interface{}) chan bool

Schedule the repeated execution of a function every `time.Duration`

Also, you can pass arguments to the function being executed via `interface`.

If no args are needed, pass an empty `interface` via: `[]interface{}{}`

Returns: `chan bool`, to stop the execution, if needed, via: stop <- true

Example
package main

import "fmt"
import "time"
import "github.com/r8k/ticker"

func print(args []interface{}) {
	fmt.Printf("Hello World !! Got %d args\n", len(args))
	for i, s := range args {
		fmt.Println(i, s)
	}
}

func main() {
	stop := ticker.TickEvery(print, time.Second*3, []interface{}{"a", 2})
	time.Sleep(time.Second * 10) // sleep for 10 seconds, to see 3 print invocations
	stop <- true                 // send a stop signal
	time.Sleep(time.Second * 2)  // sleep for 2 more seconds, to ensure, it stopped :)
}
Output:

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL