Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit26d58a5

Browse files
committed
2 parentsc07107c +88cb1f1 commit26d58a5

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
title:Throttle Function
3+
description:Ensures a function is only called at most once in a specified time interval. Useful for optimizing events like scrolling or resizing.
4+
author:WizardOfDigits
5+
tags:throttle,performance,optimization
6+
---
7+
8+
```js
9+
constthrottle= (func,limit)=> {
10+
let inThrottle;
11+
return (...args)=> {
12+
if (!inThrottle) {
13+
func(...args);
14+
inThrottle=true;
15+
setTimeout(()=> (inThrottle=false), limit);
16+
}
17+
};
18+
};
19+
20+
// Usage:
21+
// Ensures the function can only be called once every 1000 milliseconds
22+
constlogScroll=throttle(()=>console.log("Scroll event triggered"),1000);
23+
24+
// Attach to scroll event
25+
window.addEventListener("scroll", logScroll);
26+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp