Skip to content

Commit bbf3624

Browse files
committed
Sidebar: Improve performance for reize listener
1 parent 4192bcb commit bbf3624

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

assets/js/sidebar-v2.js

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,26 @@ document.addEventListener('DOMContentLoaded', () => {
5353
}
5454
});
5555

56-
window.addEventListener('resize', () => {
57-
const sidebar = document.getElementById('sidebar-v2');
56+
const debounce = (callback, wait) => {
57+
let timeoutId = null;
58+
return (...args) => {
59+
window.clearTimeout(timeoutId);
60+
timeoutId = window.setTimeout(() => {
61+
callback(...args);
62+
}, wait);
63+
};
64+
};
5865

59-
if (
60-
window.innerWidth > 88 * 16 &&
61-
sidebar.classList.contains('sidebar__mobile-open')
62-
) {
63-
sidebar.classList.remove('sidebar__mobile-open');
64-
}
65-
});
66+
window.addEventListener(
67+
'resize',
68+
debounce(() => {
69+
const sidebar = document.getElementById('sidebar-v2');
70+
71+
if (
72+
window.innerWidth > 88 * 16 &&
73+
sidebar.classList.contains('sidebar__mobile-open')
74+
) {
75+
sidebar.classList.remove('sidebar__mobile-open');
76+
}
77+
}, 200)
78+
);

0 commit comments

Comments
 (0)