-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #431 from ismail9k/340-carousel-flickers-when-resi…
…zing-it 340 carousel flickers when resizing it
- Loading branch information
Showing
3 changed files
with
25 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
/** | ||
* return a debounced version of the function | ||
* @param fn | ||
* @param delay | ||
* Returns a debounced version of the function using requestAnimationFrame. | ||
* | ||
* @param fn - The function to debounce. | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
export function debounce(fn: (...args: any[]) => unknown, delay: number): typeof fn { | ||
let timerId: ReturnType<typeof setTimeout> | null | ||
export function debounce(fn: (...args: any[]) => unknown): (...args: any[]) => void { | ||
let frameId: number | null = null | ||
|
||
return function (...args: any[]) { | ||
if (timerId) { | ||
clearTimeout(timerId) | ||
if (frameId) { | ||
cancelAnimationFrame(frameId) | ||
} | ||
timerId = setTimeout(() => { | ||
|
||
frameId = requestAnimationFrame(() => { | ||
fn(...args) | ||
timerId = null | ||
}, delay) | ||
frameId = null | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
/** | ||
* return a throttle version of the function | ||
* Throttling | ||
* Returns a throttled version of the function using requestAnimationFrame. | ||
* | ||
* @param fn - The function to throttle. | ||
*/ | ||
// eslint-disable-next-line no-unused-vars | ||
export function throttle(fn: (...args: any[]) => unknown): typeof fn { | ||
let isRunning = false | ||
export function throttle(fn: (...args: any[]) => unknown): (...args: any[]) => void { | ||
let isThrottled = false | ||
|
||
return function (...args: any[]) { | ||
if (!isRunning) { | ||
isRunning = true | ||
requestAnimationFrame(() => { | ||
fn.apply(this, args) | ||
isRunning = false | ||
}) | ||
} | ||
if (isThrottled) return | ||
|
||
isThrottled = true | ||
requestAnimationFrame(() => { | ||
fn(...args) | ||
isThrottled = false | ||
}) | ||
} | ||
} |