stifle
A very simple way to throttle a function
Last updated 7 years ago by twobitfool .
MIT · Repository · Original npm · Tarball · package.json
$ npm install stifle 
SYNC missed versions from official npm registry.

stifle

Wrap a function, so it is only called (at most) once every X milliseconds.

    var stifle = require('stifle')

    // A silly little clock
    function tellTime () {
      console.log('The time is now ' + new Date())
    }

    // Only show the time once per second
    var secondHand = stifle(tellTime, 1000)

    // Call it like crazy, but it will only fire once per second
    var interval = setInterval(secondHand, 10)

Cancellation

The wrapped function comes with a cancel method to kill any pending future invocations -- useful for shutting it down when a page or component is being unloaded.

    // Stop calling the secondHand
    clearInterval(interval)

    // Cancel pending calls, or else it will fire one more time
    secondHand.cancel()

No Extras

To keep it fast and simple, stifle does not support:

  • passing parameters
  • returning values
  • recursive invocation
  • "flushing" pending invocations
  • leading/trailing options

If you want those fancy features, check out lodash.throttle

Current Tags

  • 1.1.1                                ...           latest (5 years ago)

6 Versions

  • 1.1.1                                ...           5 years ago
  • 1.1.0                                ...           7 years ago
  • 1.0.4                                ...           7 years ago
  • 1.0.2                                ...           7 years ago
  • 1.0.1                                ...           7 years ago
  • 1.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Total 1
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE