Giter Site home page Giter Site logo

timewheel's Introduction

timewheel

golang timewheel

Go Report Card

时间轮

安装

go get -u github.com/nosixtools/timewheel

使用

package main

import (
	"fmt"
	"github.com/nosixtools/timewheel"
	"time"
)

func main() {

	//初始化时间轮盘
	//参数:interval 时间间隔
	//参数:slotNum  轮盘大小
	tw := timewheel.New(time.Second, 160)

	tw.Start()

	key := "task1"
	//添加定时任务
	//参数:interval 时间间隔
	//参数:times 执行次数 -1 表示周期任务 >0 执行指定次数
	//参数:key 任务唯一标识符 用户更新任务和删除任务
	//参数:taskData 回调函数参数
	//参数:job 回调函数
	tw.AddTask(time.Second, -1, key, timewheel.TaskData{"name": "john"},
		func(params timewheel.TaskData) {
			fmt.Println(time.Now().Unix(), params["name"])
		})

	//更新任务参数
	time.Sleep(time.Second * 10)
	tw.UpdateTask(key, time.Second*3, timewheel.TaskData{"name": "terry"})

	//删除定时任务
	time.Sleep(time.Second * 10)
	tw.RemoveTask(key)

	//轮盘停止
	tw.Stop()
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.