Skip to content

Commit

Permalink
feat(site/blog): update dependencies and optimize debounce
Browse files Browse the repository at this point in the history
  • Loading branch information
qhanw committed Sep 28, 2024
1 parent bdf9046 commit 9694630
Show file tree
Hide file tree
Showing 4 changed files with 999 additions and 1,392 deletions.
44 changes: 44 additions & 0 deletions site/blog/md/code-snippets/debounce-and-throttle.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ tags: [js]

- 防抖
```ts
// js 版本
function debounce(fn, wait = 50, immediate) {
let timer;
return function () {
Expand All @@ -19,10 +20,27 @@ function debounce(fn, wait = 50, immediate) {
}, wait);
};
}

// ts 版本
export function debounce<F extends (...args: Parameters<F>) => ReturnType<F>>(
fn: F,
wait = 50,
immediate = false,
) {
let timer: ReturnType<typeof setTimeout>;

return (...args: Parameters<F>) => {
if (immediate) fn(...args);

if (timer) clearTimeout(timer);
timer = setTimeout(() => fn(...args), wait);
};
}
```

- 节流
```ts
// js 版本
function throttle(fn, wait) {
let prev = new Date();
return function() {
Expand All @@ -34,5 +52,31 @@ function throttle(fn, wait) {
}
};
}

// ts 版本
export function throttle<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, wait = 50) {
let prev = Date.now();
return (...args: Parameters<F>) => {
const now = Date.now();
if (now - prev > wait) {
fn(...args);
prev = Date.now();
}
};
}

// ts 版本 2
export function throttle<F extends (...args: Parameters<F>) => ReturnType<F>>(fn: F, wait = 50) {
let inThrottle = false;

return function (...args: Parameters<F>) {
if (!inThrottle) {
inThrottle = true;
setTimeout(() => (inThrottle = false), wait);

fn(...args);
}
};
}
```

2 changes: 1 addition & 1 deletion site/blog/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
32 changes: 16 additions & 16 deletions site/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
},
"dependencies": {
"@amap/amap-jsapi-loader": "^1.0.1",
"@shikijs/rehype": "^1.13.0",
"@shikijs/rehype": "^1.20.0",
"clsx": "^2.1.1",
"dayjs": "^1.11.12",
"dayjs": "^1.11.13",
"gray-matter": "^4.0.3",
"next": "14.2.5",
"next": "14.2.13",
"next-mdx-remote": "^5.0.0",
"nextjs-toploader": "^1.6.12",
"nextjs-toploader": "^3.6.15",
"photoswipe": "^5.4.4",
"plyr": "^3.7.8",
"react": "18.3.1",
Expand All @@ -26,27 +26,27 @@
"reading-time": "^1.5.0",
"rehype-autolink-headings": "^7.1.0",
"rehype-slug": "^6.0.0",
"rehype-stringify": "^10.0.0",
"rehype-stringify": "^10.0.1",
"remark": "^15.0.1",
"remark-gfm": "^4.0.0",
"remark-gh-alerts": "^0.0.3",
"remark-rehype": "^11.1.0",
"remark-rehype": "^11.1.1",
"remark-toc": "^9.0.0"
},
"devDependencies": {
"@iconify-json/heroicons": "^1.1.24",
"@iconify-json/ri": "^1.1.22",
"@types/node": "22.3.0",
"@types/react": "18.3.3",
"@iconify-json/heroicons": "^1.2.0",
"@iconify-json/ri": "^1.2.0",
"@types/node": "22.7.4",
"@types/react": "18.3.10",
"@types/react-dom": "18.3.0",
"@unocss/postcss": "^0.62.2",
"@unocss/postcss": "^0.63.0",
"@vercel/analytics": "^1.3.1",
"autoprefixer": "10.4.20",
"eslint": "8.57.0",
"eslint-config-next": "14.2.5",
"postcss": "8.4.41",
"sass": "^1.77.8",
"typescript": "5.5.4",
"unocss": "^0.62.2"
"eslint-config-next": "14.2.13",
"postcss": "8.4.47",
"sass": "^1.79.4",
"typescript": "5.6.2",
"unocss": "^0.63.0"
}
}
Loading

0 comments on commit 9694630

Please sign in to comment.