Add debounce functionality to resize event in Background component

- Implemented a debounce mechanism for the window resize event to optimize performance.
- The state is updated with the new page width after a delay of 300ms following the last resize event.
This commit is contained in:
troyeguo
2026-04-14 10:07:05 +08:00
parent 529bc0cc19
commit a3fa2d0ce6
2 changed files with 16 additions and 1 deletions

View File

File diff suppressed because one or more lines are too long

View File

@@ -24,6 +24,21 @@ class Background extends React.Component<BackgroundProps, BackgroundState> {
this.props.isSettingLocked
)
);
let resizeTimer: NodeJS.Timeout;
window.addEventListener("resize", (event) => {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(() => {
this.setState(
getPageWidth(
this.props.readerMode,
this.props.scale,
parseInt(this.props.margin),
this.props.isNavLocked,
this.props.isSettingLocked
)
);
}, 300); // 300ms 防抖
});
}
async UNSAFE_componentWillReceiveProps(nextProps: BackgroundProps) {
if (