interval

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

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

Go to latest
Published: Dec 7, 2019 License: MIT Imports: 1 Imported by: 6

README

interval

GoDoc

Repeatedly call a function with a fixed time delay between each call. This is an alternative to the time.Ticker.

Usage

There are only two functions Set() and Clear(). The Set function begins repeating the function until Clear is called.

Functions
func Set(fn func(t time.Time), delay time.Duration) Interval
func (iv Interval) Clear()
Example

Let's say that you have a custom file type which needs to perform a background operation every hour.

This example starts the interval function when the object opens and clears the interval when the object is closed.


type SpecialFile struct {
    iv Interval
}

func Open(path string) (*SpecialFile, error) {
    f := new(SpecialFile)
    f.iv = interval.Start(func(t time.Time){
        db.bgOperation()
    }, time.Hour)
    return f, nil
}

func Close() error{
    f.iv.Clear()
    return nil
}

func (f *SpecialFile) bgOperation() {
    // this operation runs in the backgound
}

Contact

Josh Baker @tidwall

License

interval source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interval

type Interval struct {
	// contains filtered or unexported fields
}

Interval represents a repeatedly called a function with fixed time delay. Use Start() to start running an interval.

func Set

func Set(fn func(t time.Time), delay time.Duration) Interval

Set repeatedly calls a function with a fixed time delay between each call. Returns an interval object which can be later removed by calling Clear().

func (Interval) Clear

func (iv Interval) Clear()

Clear cancels the repeating action which was previously established by Set(). This operation waits for an active action to complete before returning. This function should never be called from inside of the repeated function, otherwise the app will block.

Jump to

Keyboard shortcuts

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