{"id":15705996,"url":"https://github.com/czzonet/debounce","last_synced_at":"2025-03-30T15:45:58.088Z","repository":{"id":118158692,"uuid":"206753123","full_name":"czzonet/debounce","owner":"czzonet","description":"debounce || 函数防抖","archived":false,"fork":false,"pushed_at":"2019-09-07T02:11:41.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-05T17:25:45.287Z","etag":null,"topics":["debounce","typescript"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/czzonet.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-09-06T08:49:27.000Z","updated_at":"2019-09-07T02:11:43.000Z","dependencies_parsed_at":"2023-11-10T13:06:05.291Z","dependency_job_id":null,"html_url":"https://github.com/czzonet/debounce","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czzonet%2Fdebounce","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czzonet%2Fdebounce/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czzonet%2Fdebounce/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/czzonet%2Fdebounce/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/czzonet","download_url":"https://codeload.github.com/czzonet/debounce/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246342824,"owners_count":20761939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["debounce","typescript"],"created_at":"2024-10-03T20:20:59.624Z","updated_at":"2025-03-30T15:45:58.062Z","avatar_url":"https://github.com/czzonet.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Debounce || 函数防抖\n=\n\n函数防抖，用一个命令行cli演示，输入过快回车也只会最后调用一次处理逻辑。 :white_check_mark:\n\n**原理；** 用闭包存储上一个计时器id，然后子函数先清除该id，再设置新id。\n\n**本质：** 闭包作用和简单的类一样，存储功能如同私有成员变量。\n\n具体实现在src/lib里。\n\n演示\n--\n\n![](debounce.gif)\n\n运行\n--\n\n```sh\nnpm run dev\n```\n\n具体实现\n--\n\n定义工厂函数\n```ts\nconst debounce = (fn: () =\u003e any, timeout: number) =\u003e {\n  let timmer: any;\n  return () =\u003e {\n    timmer ? clearTimeout(timmer) : null\n    timmer = setTimeout(fn, timeout);\n  }\n}\n```\n\n生成防抖函数，配置处理逻辑和延时\n\n```ts\nconst debouncer = debounce(() =\u003e console.log('debounce'), 2000)\n```\n\n调用防抖函数\n\n```ts\ndebouncer()\n```\n\nReferences\n--\n\n1. [闭包 - JavaScript | MDN](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Closures)\n2. [函数防抖与函数节流 - 前端 - 掘金](https://juejin.im/entry/5b2b3f6ee51d4558df36fb93)\n3. [Timers in Node.js | Node.js](https://nodejs.org/de/docs/guides/timers-in-node/)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczzonet%2Fdebounce","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fczzonet%2Fdebounce","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fczzonet%2Fdebounce/lists"}