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

@lifechat/debounce-throttle

v1.0.7

Published

[![npm bundle size (minified + gzip)](https://img.shields.io/bundlephobia/minzip/ts-debounce-throttle.svg)](https://www.npmjs.com/package/ts-debounce-throttle) [![npm type definitions](https://img.shields.io/npm/types/ts-debounce-throttle.svg)](https://ww

Downloads

4

Readme

TypeScript implementation of debounce and throttle function

npm bundle size (minified + gzip) npm type definitions

Debounce 创建了一个新的函数 g,当它被调用时,会将原函数 f 的调用延迟到 n 毫秒,但如果在 n 毫秒之前有新的调用,就会放弃之前的延迟排放。

在限制函数调用次数的情况下,这是非常有用的。例如,考虑从 API 获取数据的搜索输入。在用户停止输入字符一段时间后显示搜索结果就足够了。

Install

你可以使用npm来安装这个软件包,命令如下

npm i @lifechat/debounce-throttle

如果你喜欢`yarn',可以用下面的命令安装

yarn add @lifechat/debounce-throttle

函数参数

import { debounce } from '@lifechat/debounce-throttle'

const debouncedFunction = debounce(originalFunction, waitMilliseconds, options)
  • originalFunction
    • 我们需要 debounce 的函数
  • waitMilliseconds
    • 在最近一次函数调用后,必须经过多少秒,才能调用原始函数
  • options
    • isImmediate (boolean)
      • 如果设置为 "true",那么 "originalFunction "将被立即调用,但在随后的调用中,原始函数将不会被调用,除非 "waitMilliseconds "在最后一次调用后通过。
    • maxWait (number)
      • 如果定义了,它将在maxWait'时间过后调用originalFunction',即使在此期间调用了 debounced 函数。
        • 例如,如果wait被设置为100maxWait被设置为200,那么如果每隔 50ms 调用一次 debounced 函数,那么原始函数将在 200ms 后被调用。
    • callback (function)
      • 当 "originalFunction "被释放时,它被调用,并接收 "originalFunction "返回的数据作为第一个参数

撤销

可以通过对其调用cancel()来取消返回的 debounced 函数。

const debouncedFunction = debounce(originalFunction, waitMilliseconds, options)

debouncedFunction.cancel()

Promises

每次你调用 debounced 函数时,都会返回一个 Promise,这个 Promise 将在原始函数被最终调用时被 resolve。如果 debounced 函数被取消了,这个 Promise 将被 reject 掉。 你也可以去掉一个返回承诺的函数 f。返回的 Promise 将与原始函数的返回值一起解析(除非被取消)。

const asyncFunction = async () => 'value'
const g = debounce(asyncFunction)
const returnValue = await g()
returnValue === 'value' // true