timer

package
v0.0.0-...-04cfb28 Latest Latest
Warning

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

Go to latest
Published: May 22, 2020 License: MIT Imports: 6 Imported by: 0

README

一、定时器

package main

import (
	"fmt"
	"github.com/nothollyhigh/kiss/timer"
	"time"
)

func main() {
	t1 := time.Now()
	<-timer.After(time.Second)
	fmt.Println("timer.After 111 sec:", time.Since(t1).Seconds())

	t2 := time.Now()
	timer.Once(time.Second, func() {
		fmt.Println("timer.Once after 222 sec:", time.Since(t2).Seconds())
	})
	time.Sleep(time.Second)

	t3 := time.Now()
	timer.AfterFunc(time.Second, func() {
		fmt.Println("timer.AfterFunc after 333 sec:", time.Since(t3).Seconds())
	})
	time.Sleep(time.Second)

	t4 := time.Now()
	timer.Schedule(time.Second, time.Second, 3, func() {
		fmt.Println("timer.Schedule after 444 sec:", time.Since(t4).Seconds())
		t4 = time.Now()
	})
	time.Sleep(time.Second * 4)

	t5 := time.Now()
	item := timer.Schedule(time.Second, time.Second, 3, func() {
		fmt.Println("timer.Schedule 555 cancel after 1.5 s, sec:", time.Since(t5).Seconds())
		t5 = time.Now()
	})
	time.Sleep(time.Second / 2 * 3)
	item.Cancel()

	time.Sleep(time.Second * 5)
	fmt.Println("over")
}

Documentation

Index

Constants

View Source
const (
	TIME_FOREVER = time.Duration(math.MaxInt64)
)

Variables

View Source
var (
	DefaultTimer = New("default")
)

Functions

func After

func After(timeout time.Duration) <-chan struct{}

block timeout

func Cancel

func Cancel(item *TimerItem)

cancel timer

Types

type Timer

type Timer struct {
	sync.Mutex
	// contains filtered or unexported fields
}

timer

func New

func New(tag string) *Timer

timer factory

func (*Timer) After

func (tm *Timer) After(timeout time.Duration) <-chan struct{}

block timeout

func (*Timer) AfterFunc

func (tm *Timer) AfterFunc(timeout time.Duration, cb func()) *TimerItem

aysnc callback after timeout

func (*Timer) Cancel

func (tm *Timer) Cancel(item *TimerItem)

cancel timer

func (*Timer) Once

func (tm *Timer) Once(timeout time.Duration, cb func()) *TimerItem

async callback for once after timeout

func (*Timer) Reset

func (tm *Timer) Reset(item *TimerItem, delay time.Duration)

reset

func (*Timer) Schedule

func (tm *Timer) Schedule(delay time.Duration, interval time.Duration, repeat int64, cb func()) *TimerItem

async callback loop task

func (*Timer) Size

func (tm *Timer) Size() int

size

func (*Timer) Stop

func (tm *Timer) Stop()

stop

type TimerItem

type TimerItem struct {
	Index    int
	Expire   time.Time
	Callback func()
	Parent   *Timer
}

timer item

func AfterFunc

func AfterFunc(timeout time.Duration, cb func()) *TimerItem

async callback once after timeout

func Once

func Once(timeout time.Duration, cb func()) *TimerItem

async callback once after timeout

func Schedule

func Schedule(delay time.Duration, interval time.Duration, repeat int64, cb func()) *TimerItem

async callback loop task

func (*TimerItem) Cancel

func (it *TimerItem) Cancel()

cancel timer

func (*TimerItem) Reset

func (it *TimerItem) Reset(delay time.Duration)

reset timer

type Timers

type Timers []*TimerItem

timer array for heap sort

func (*Timers) Head

func (tm *Timers) Head() *TimerItem

heap head

func (Timers) Len

func (tm Timers) Len() int

length

func (Timers) Less

func (tm Timers) Less(i, j int) bool

less

func (*Timers) Pop

func (tm *Timers) Pop() interface{}

pop

func (*Timers) Push

func (tm *Timers) Push(x interface{})

push

func (*Timers) Remove

func (tm *Timers) Remove(i int)

remove

func (Timers) Swap

func (tm Timers) Swap(i, j int)

swap

Jump to

Keyboard shortcuts

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