npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

debounce-ctx

v1.0.3

Published

A simple debounce utility that optionally takes a specific context

Downloads

8

Readme

debounce-ctx

A simple debounce utility that optionally takes a specific context

Install

npm install debounce-ctx

Usage

debounce(f, wait, ctx)

<script src="debounce-ctx.min.js"></script>
<script>
window.addEventListener('resize', debounce(resize, 400));
</script>

(for TypeScript users, refer Notes bellow)

or

const debounce = require('debounce-ctx');
window.addEventListener('resize', debounce(resize, 400));

If you want to bind the function to a certain context, you can either simply bind it:

var ctx = new MyClass();
window.addEventListener('resize', debounce(resize.bind(ctx), 400));

or, you can give the context as the third argument:

var ctx = new MyClass();
window.addEventListener('resize', debounce(resize, 400, ctx));

Test

Testing CommonJS

node ./test/cjs/test.js

Testing UMD

Firstly, build the bundle for browser:

yarn build

Launch your browser, open test/umd/index.html, and click the button to see if it works.

Notes

(a) TypeScript: "import/export"

You may encounter several errors when you attempt to use the module with our TypeScript based projects depending on the version of TypeScript you use.

TS1259: Module '"/home/minagawah/local/workspace/git/my-simple-webpack-typescript/node_modules/debounce-ctx/index"' can only be default-imported using the 'allowSyntheticDefaultImports' flag

Referencing exteral modules using TypeScript could be often a time tricky. For the above error, import syntax of ES6 does not reference default export. To resolve the issue, if you are using v2.7 or over, you may use --esModuleInterop flag as you compile your TypeScript projects, or specify the flag in your tsconfig.json.

"compilerOptions": {
  "esModuleInterop": true
}

https://stackoverflow.com/questions/54701255/importing-victor-js-in-typescript#answer-54785103

(b) TypeScript: "addEventListener"

TS2345: Argument of type 'Function' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
  Property 'handleEvent' is missing in type 'Function' but required in type 'EventListenerObject'.

From v2.6, TypeScript no longer accept event if it is not CustomEvent type.
There are several workarounds for this, but the easiest would be to tell TypeScript compiler by casting any to the element you deal with to not to bother with the type.

(<any>window).addEventListener('resize', debounce(reset, 400), false);

https://stackoverflow.com/questions/46925133/how-to-add-passive-option-to-addeventlistener-in-typescript#answer-46925923

Or, if you want to be more strict about it, consider the options presented in:

https://stackoverflow.com/questions/47166369/argument-of-type-e-customevent-void-is-not-assignable-to-parameter-of-ty?rq=1#answer-47171216

License

MIT License
Copyright (c) 2019-2020 Hiroki Minagawa

See LICENSE for details.