{"id":2622,"date":"2026-01-22T15:18:22","date_gmt":"2026-01-22T07:18:22","guid":{"rendered":"https:\/\/womeifei.cn\/?p=2622"},"modified":"2026-01-22T15:20:21","modified_gmt":"2026-01-22T07:20:21","slug":"debounce-throttle-function","status":"publish","type":"post","link":"https:\/\/womeifei.cn\/index.php\/2026\/01\/22\/debounce-throttle-function\/","title":{"rendered":"debounce &amp; throttle function"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">debounce<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u9632\u6296\u51fd\u6570 (Debounce)\n * @param {Function} fn - \u9700\u8981\u6267\u884c\u7684\u51fd\u6570\n * @param {Number} delay - \u5ef6\u8fdf\u65f6\u95f4(ms)\n *\/\nfunction debounce(fn, delay) {\n    let timer = null; \/\/ 1. \u95ed\u5305\u4fdd\u5b58\u5b9a\u65f6\u5668\n\n    return function(...args) {\n        \/\/ 2. \u5982\u679c\u5b9a\u65f6\u5668\u8fd8\u5728\uff0c\u8bf4\u660e\u4e0a\u4e00\u6b21\u8fd8\u6ca1\u6267\u884c\u5b8c\uff0c\u6e05\u9664\u5b83\n        if (timer) clearTimeout(timer);\n\n        \/\/ 3. \u91cd\u65b0\u5f00\u542f\u5b9a\u65f6\u5668\uff0cdelay \u65f6\u95f4\u540e\u6267\u884c\n        timer = setTimeout(() =&gt; {\n            \/\/ 4. \u8fd9\u91cc\u7684\u7bad\u5934\u51fd\u6570\u81ea\u52a8\u7a7f\u900f\uff0c\u62ff\u5230\u5916\u5c42\u7684 this\n            fn.apply(this, args);\n        }, delay);\n    }\n}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/womeifei.cn\/wp-content\/uploads\/2026\/01\/image-39-1024x598.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"598\" data-original=\"https:\/\/womeifei.cn\/wp-content\/uploads\/2026\/01\/image-39-1024x598.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-2624\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">throttle<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\n * \u8282\u6d41\u51fd\u6570 (Throttle)\n * @param {Function} fn - \u9700\u8981\u6267\u884c\u7684\u51fd\u6570\n * @param {Number} delay - \u95f4\u9694\u65f6\u95f4(ms)\n *\/\nfunction throttle(fn, delay) {\n    let timer = null; \/\/ 1. \u95ed\u5305\u4fdd\u5b58\u5b9a\u65f6\u5668\u6807\u5fd7\n\n    return function(...args) {\n        \/\/ 2. \u5982\u679c\u5b9a\u65f6\u5668\u5b58\u5728\uff0c\u8bf4\u660e\u8fd8\u5728\"\u51b7\u5374\u4e2d\"\uff0c\u76f4\u63a5\u8fd4\u56de\n        if (timer) return;\n\n        \/\/ 3. \u5f00\u542f\u5b9a\u65f6\u5668\uff08\u4e0a\u9501\uff09\n        timer = setTimeout(() =&gt; {\n            \/\/ 4. \u65f6\u95f4\u5230\u4e86\uff0c\u6267\u884c\u51fd\u6570\n            fn.apply(this, args);\n            \n            \/\/ 5. \u5173\u952e\uff1a\u6267\u884c\u5b8c\u540e\uff0c\u628a\u5b9a\u65f6\u5668\u6e05\u7a7a\uff08\u89e3\u9501\uff09\uff0c\u5141\u8bb8\u4e0b\u4e00\u6b21\u89e6\u53d1\n            timer = null;\n        }, delay);\n    }\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Example Code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;!DOCTYPE html&gt;\n&lt;html lang=\"zh-CN\"&gt;\n\n&lt;head&gt;\n    &lt;meta charset=\"UTF-8\"&gt;\n    &lt;meta\n        name=\"viewport\"\n        content=\"width=device-width, initial-scale=1.0\"\n    &gt;\n    &lt;title&gt;Debounce &amp; Throttle&lt;\/title&gt;\n    &lt;style&gt;\n        body {\n            font-family: sans-serif;\n            padding: 20px;\n            line-height: 1.6;\n        }\n\n        .container {\n            margin-bottom: 30px;\n            padding: 20px;\n            border: 1px solid #ddd;\n            border-radius: 8px;\n        }\n\n        .box {\n            width: 100%;\n            height: 200px;\n            background-color: #f0f4f8;\n            display: flex;\n            align-items: center;\n            justify-content: center;\n            font-size: 20px;\n            color: #333;\n            cursor: crosshair;\n            border: 2px dashed #007bff;\n        }\n\n        input {\n            padding: 10px;\n            width: 300px;\n            font-size: 16px;\n        }\n\n        strong {\n            color: #d9534f;\n        }\n    &lt;\/style&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n    &lt;h1&gt;\u524d\u7aef\u9762\u8bd5\uff1a\u624b\u5199\u9632\u6296\u4e0e\u8282\u6d41&lt;\/h1&gt;\n\n    &lt;div class=\"container\"&gt;\n        &lt;h3&gt;1. \u9632\u6296 (Debounce) \u793a\u4f8b&lt;\/h3&gt;\n        &lt;p&gt;\u573a\u666f\uff1a\u641c\u7d22\u6846\u3002\u8bf7\u5728\u4e0b\u65b9\u8fde\u7eed\u8f93\u5165\u6587\u5b57\uff0c\u89c2\u5bdf \"\u89e6\u53d1\u7ed3\u679c\"\u3002&lt;\/p&gt;\n        &lt;input\n            type=\"text\"\n            id=\"debounceInput\"\n            placeholder=\"\u5c1d\u8bd5\u8fde\u7eed\u5feb\u901f\u8f93\u5165...\"\n        &gt;\n        &lt;p&gt;\u5b9e\u9645\u89e6\u53d1\u7ed3\u679c\uff1a&lt;strong id=\"debounceResult\"&gt;\u7b49\u5f85\u8f93\u5165...&lt;\/strong&gt;&lt;\/p&gt;\n    &lt;\/div&gt;\n\n    &lt;div class=\"container\"&gt;\n        &lt;h3&gt;2. \u8282\u6d41 (Throttle) \u793a\u4f8b&lt;\/h3&gt;\n        &lt;p&gt;\u573a\u666f\uff1a\u9f20\u6807\u79fb\u52a8\u3002\u8bf7\u5728\u4e0b\u65b9\u84dd\u8272\u533a\u57df\u5185\u5feb\u901f\u79fb\u52a8\u9f20\u6807\u3002&lt;\/p&gt;\n        &lt;div\n            id=\"throttleBox\"\n            class=\"box\"\n        &gt;\n            \u9f20\u6807\u5728\u8fd9\u91cc\u79fb\u52a8\n        &lt;\/div&gt;\n        &lt;p&gt;\u8ba1\u6570\u5668 (\u6bcf1\u79d2\u66f4\u65b0\u4e00\u6b21)\uff1a&lt;strong id=\"throttleResult\"&gt;0&lt;\/strong&gt;&lt;\/p&gt;\n    &lt;\/div&gt;\n\n    &lt;script&gt;\n        \/\/ ==============================\n        \/\/ 1. \u624b\u5199\u9632\u6296\u51fd\u6570\n        \/\/ ==============================\n\n\n        function debounce(fn, delay) {\n            let timer = null\n            return function (...args) {\n                if (timer) clearTimeout(timer)\n                timer = setTimeout(() =&gt; {\n                    fn.apply(this, args)\n                }, delay)\n            }\n        }\n\n        \/\/ ==============================\n        \/\/ 2. \u624b\u5199\u8282\u6d41\u51fd\u6570\n        \/\/ ==============================\n        function throttle(fn, delay) {\n            let timer = null\n            return function (...args) {\n                if (timer) return  \/\/\u51b7\u5374\u4e2d \u4e0d\u6267\u884c\n                timer = setTimeout(() =&gt; {\n                    fn.apply(this, args)\n                    timer = null \/\/\u89e3\u9501\n                }, delay)\n            }\n        }\n\n        \/\/ ==============================\n        \/\/ \u4e1a\u52a1\u903b\u8f91\u7ed1\u5b9a\n        \/\/ ==============================\n\n        \/\/ 1. \u9632\u6296\u6d4b\u8bd5\u903b\u8f91\n        const inputEl = document.getElementById('debounceInput');\n        const debounceResultEl = document.getElementById('debounceResult');\n\n        \/\/ \u5b9a\u4e49\u8981\u6267\u884c\u7684\u4e1a\u52a1\u51fd\u6570\n        function handleInput(e) {\n            console.log('\u9632\u6296\u89e6\u53d1:', e.target.value);\n            debounceResultEl.innerText = `\u53d1\u9001\u8bf7\u6c42: ${e.target.value} (${new Date().toLocaleTimeString()})`;\n        }\n\n        \/\/ \u7ed1\u5b9a\u4e8b\u4ef6\uff1a\u5ef6\u8fdf 1000ms \u6267\u884c\n        inputEl.addEventListener('input', debounce(handleInput, 1000));\n\n        \/\/ 2. \u8282\u6d41\u6d4b\u8bd5\u903b\u8f91\n        const boxEl = document.getElementById('throttleBox');\n        const throttleResultEl = document.getElementById('throttleResult');\n        let count = 0;\n\n        \/\/ \u5b9a\u4e49\u8981\u6267\u884c\u7684\u4e1a\u52a1\u51fd\u6570\n        function handleMouseMove(e) {\n            count++;\n            console.log('\u8282\u6d41\u89e6\u53d1 count:', count);\n            throttleResultEl.innerText = count;\n            boxEl.innerText = `X: ${e.offsetX}, Y: ${e.offsetY}`;\n        }\n\n        \/\/ \u7ed1\u5b9a\u4e8b\u4ef6\uff1a\u6bcf 1000ms \u6700\u591a\u6267\u884c\u4e00\u6b21\n        boxEl.addEventListener('mousemove', throttle(handleMouseMove, 1000));\n\n    &lt;\/script&gt;\n&lt;\/body&gt;\n\n&lt;\/html&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>debounce throttle Example Code<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,10,1,46],"tags":[],"class_list":["post-2622","post","type-post","status-publish","format-standard","hentry","category-html5css3-","category-javascript","category-1","category-46"],"_links":{"self":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2622"}],"version-history":[{"count":2,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2622\/revisions"}],"predecessor-version":[{"id":2625,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/posts\/2622\/revisions\/2625"}],"wp:attachment":[{"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/womeifei.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}