awesome-interview/assets/js/d9cd0856.3da8a59a.js

1 line
7.4 KiB
JavaScript

"use strict";(self.webpackChunkjjbook=self.webpackChunkjjbook||[]).push([[6238],{3905:function(e,t,n){n.d(t,{Zo:function(){return s},kt:function(){return d}});var r=n(7294);function i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function l(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){i(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function a(e,t){if(null==e)return{};var n,r,i=function(e,t){if(null==e)return{};var n,r,i={},o=Object.keys(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||(i[n]=e[n]);return i}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(r=0;r<o.length;r++)n=o[r],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(i[n]=e[n])}return i}var u=r.createContext({}),c=function(e){var t=r.useContext(u),n=t;return e&&(n="function"==typeof e?e(t):l(l({},t),e)),n},s=function(e){var t=c(e.components);return r.createElement(u.Provider,{value:t},e.children)},p={inlineCode:"code",wrapper:function(e){var t=e.children;return r.createElement(r.Fragment,{},t)}},m=r.forwardRef((function(e,t){var n=e.components,i=e.mdxType,o=e.originalType,u=e.parentName,s=a(e,["components","mdxType","originalType","parentName"]),m=c(n),d=i,f=m["".concat(u,".").concat(d)]||m[d]||p[d]||o;return n?r.createElement(f,l(l({ref:t},s),{},{components:n})):r.createElement(f,l({ref:t},s))}));function d(e,t){var n=arguments,i=t&&t.mdxType;if("string"==typeof e||i){var o=n.length,l=new Array(o);l[0]=m;var a={};for(var u in t)hasOwnProperty.call(t,u)&&(a[u]=t[u]);a.originalType=e,a.mdxType="string"==typeof e?e:i,l[1]=a;for(var c=2;c<o;c++)l[c]=n[c];return r.createElement.apply(null,l)}return r.createElement.apply(null,n)}m.displayName="MDXCreateElement"},2734:function(e,t,n){n.r(t),n.d(t,{frontMatter:function(){return a},contentTitle:function(){return u},metadata:function(){return c},toc:function(){return s},default:function(){return m}});var r=n(7462),i=n(3366),o=(n(7294),n(3905)),l=["components"],a={sidebar_label:"\u7f16\u7801\uff1a\u5b9e\u73b0\u8282\u6d41\u53bb\u6296\u51fd\u6570",sidebar_position:10},u="\u5b9e\u73b0\u8282\u6d41\u53bb\u6296\u51fd\u6570",c={unversionedId:"book2/coding-throttle-debounce",id:"book2/coding-throttle-debounce",isDocsHomePage:!1,title:"\u5b9e\u73b0\u8282\u6d41\u53bb\u6296\u51fd\u6570",description:"\u8282\u6d41",source:"@site/docs/book2/coding-throttle-debounce.md",sourceDirName:"book2",slug:"/book2/coding-throttle-debounce",permalink:"/awesome-interview/book2/coding-throttle-debounce",tags:[],version:"current",sidebarPosition:10,frontMatter:{sidebar_label:"\u7f16\u7801\uff1a\u5b9e\u73b0\u8282\u6d41\u53bb\u6296\u51fd\u6570",sidebar_position:10},sidebar:"tutorialSidebar",previous:{title:"\u7f51\u7edc\uff1aHTTP \u7f13\u5b58\u673a\u5236",permalink:"/awesome-interview/book2/network-http-cache"},next:{title:"\u7b97\u6cd5\uff1a\u53cd\u8f6c\u94fe\u8868",permalink:"/awesome-interview/book2/algorithm-reverse-linked-list"}},s=[{value:"\u8282\u6d41",id:"\u8282\u6d41",children:[{value:"1. \u57fa\u672c\u6982\u5ff5",id:"1-\u57fa\u672c\u6982\u5ff5",children:[]},{value:"2. \u5e94\u7528\u573a\u666f",id:"2-\u5e94\u7528\u573a\u666f",children:[]},{value:"3. \u6d41\u7a0b\u56fe",id:"3-\u6d41\u7a0b\u56fe",children:[]},{value:"4. \u7f16\u5199\u4ee3\u7801",id:"4-\u7f16\u5199\u4ee3\u7801",children:[]}]},{value:"\u53bb\u6296",id:"\u53bb\u6296",children:[{value:"1. \u57fa\u672c\u6982\u5ff5",id:"1-\u57fa\u672c\u6982\u5ff5-1",children:[]},{value:"2. \u5e94\u7528\u573a\u666f",id:"2-\u5e94\u7528\u573a\u666f-1",children:[]},{value:"3. \u6d41\u7a0b\u56fe",id:"3-\u6d41\u7a0b\u56fe-1",children:[]},{value:"4. \u7f16\u5199\u4ee3\u7801",id:"4-\u7f16\u5199\u4ee3\u7801-1",children:[]}]}],p={toc:s};function m(e){var t=e.components,n=(0,i.Z)(e,l);return(0,o.kt)("wrapper",(0,r.Z)({},p,n,{components:t,mdxType:"MDXLayout"}),(0,o.kt)("h1",{id:"\u5b9e\u73b0\u8282\u6d41\u53bb\u6296\u51fd\u6570"},"\u5b9e\u73b0\u8282\u6d41\u53bb\u6296\u51fd\u6570"),(0,o.kt)("h2",{id:"\u8282\u6d41"},"\u8282\u6d41"),(0,o.kt)("h3",{id:"1-\u57fa\u672c\u6982\u5ff5"},"1. \u57fa\u672c\u6982\u5ff5"),(0,o.kt)("p",null,(0,o.kt)("inlineCode",{parentName:"p"},"throttle(func, wait)")),(0,o.kt)("p",null,"\u6bcf wait \u6beb\u79d2\u5185\u6700\u591a\u53ea\u8c03\u7528\u4e00\u6b21 func\u3002"),(0,o.kt)("h3",{id:"2-\u5e94\u7528\u573a\u666f"},"2. \u5e94\u7528\u573a\u666f"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"\u641c\u7d22\u6846\u8f93\u5165\u65f6\u7684\u5b9e\u65f6\u8054\u60f3\u3002"),(0,o.kt)("li",{parentName:"ul"},"\u76d1\u542c scroll \u4e8b\u4ef6\u8ba1\u7b97\u4f4d\u7f6e\u4fe1\u606f\u3002")),(0,o.kt)("h3",{id:"3-\u6d41\u7a0b\u56fe"},"3. \u6d41\u7a0b\u56fe"),(0,o.kt)("p",null,(0,o.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/17525377/126268731-dc74499d-6ed5-408b-b1a8-60c24c77babc.jpg",alt:"\u8282\u6d41"})),(0,o.kt)("h3",{id:"4-\u7f16\u5199\u4ee3\u7801"},"4. \u7f16\u5199\u4ee3\u7801"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js"},"function throttle(func, wait) {\n let lastTime = 0;\n let timer = null;\n\n return function () {\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n\n let self = this;\n let args = arguments;\n let nowTime = +new Date();\n\n const remainWaitTime = wait - (nowTime - lastTime);\n\n if (remainWaitTime <= 0) {\n lastTime = nowTime;\n func.apply(self, args);\n } else {\n timer = setTimeout(function () {\n lastTime = +new Date();\n func.apply(self, args);\n timer = null;\n }, remainWaitTime);\n }\n };\n}\n")),(0,o.kt)("h2",{id:"\u53bb\u6296"},"\u53bb\u6296"),(0,o.kt)("h3",{id:"1-\u57fa\u672c\u6982\u5ff5-1"},"1. \u57fa\u672c\u6982\u5ff5"),(0,o.kt)("p",null,(0,o.kt)("inlineCode",{parentName:"p"},"debounce(func, wait)")),(0,o.kt)("p",null,"\u81ea\u6700\u8fd1\u4e00\u6b21\u89e6\u53d1\u540e\u5ef6\u8fdf wait \u6beb\u79d2\u8c03\u7528 func\u3002"),(0,o.kt)("h3",{id:"2-\u5e94\u7528\u573a\u666f-1"},"2. \u5e94\u7528\u573a\u666f"),(0,o.kt)("ul",null,(0,o.kt)("li",{parentName:"ul"},"\u6ce8\u518c\u65f6\u8f93\u5165\u5b8c\u7528\u6237\u540d\u540e\u68c0\u6d4b\u662f\u5426\u88ab\u5360\u7528\u3002"),(0,o.kt)("li",{parentName:"ul"},"\u76d1\u542c resize \u4e8b\u4ef6\u8ba1\u7b97\u5c3a\u5bf8\u4fe1\u606f\u3002")),(0,o.kt)("h3",{id:"3-\u6d41\u7a0b\u56fe-1"},"3. \u6d41\u7a0b\u56fe"),(0,o.kt)("p",null,(0,o.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/17525377/126269981-e85e1bd2-98f3-4375-b361-696d63be8505.jpg",alt:"\u9632\u6296"})),(0,o.kt)("h3",{id:"4-\u7f16\u5199\u4ee3\u7801-1"},"4. \u7f16\u5199\u4ee3\u7801"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js"},"function debounce(func, wait) {\n let timer = null;\n\n return function () {\n if (timer) {\n clearTimeout(timer);\n timer = null;\n }\n\n let self = this;\n let args = arguments;\n\n timer = setTimeout(function () {\n func.apply(self, args);\n timer = null;\n }, wait);\n };\n}\n")))}m.isMDXComponent=!0}}]);