Package detail

typescript-debounce-decorator

duxiaofeng-github25.1kMIT0.0.18

A debounce decorator for typescript class method

typescript, debounce, decorator, typescript-debounce-decorator

readme

typescript-debounce-decorator

NPM version NPM downloads Build status

A debounce decorator for typescript class method

  • Tiny (1KB after uglify compressed)
  • No dependency
  • Easy to use

Install

npm install typescript-debounce-decorator --save

Usage

Syntax:

@debounce(debounceTime, options)

Params:

  • debounceTime: number Function execute interval in milliseconds.
  • options: object Options.
    • leading: boolean Should function invoke on the leading or trailing of the wait timeout.

NOTE: Return value of function which applied debounce decorator will be eaten.

Basic usage:

import { debounce } from "typescript-debounce-decorator";

class Foo {
    @debounce
    bar() {
        console.log("foobar");
    }
}

With debounce time:

import { debounce } from "typescript-debounce-decorator";

class Foo {
    @debounce(1000)
    bar() {
        console.log("foobar");
    }
}

With options:

import { debounce } from "typescript-debounce-decorator";

class Foo {
    @debounce(1000, { leading: true })
    bar() {
        console.log("foobar");
    }
}

Cancel:

import { debounce, cancel } from "typescript-debounce-decorator";

class Foo {
    @debounce(1000, { leading: true })
    bar() {
        console.log("foobar");
    }

    cancel() {
        cancel(this.bar);
    }
}

Changelog

  • 0.0.18: [BREAKCHANGE] leading option now default to false

License

MIT