mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-13 07:46:17 -08:00
33 lines
710 B
JavaScript
33 lines
710 B
JavaScript
export function sleep(ms = 0) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
|
|
export function debounce(func, duration) {
|
|
let timeout;
|
|
|
|
return function (...args) {
|
|
const effect = () => {
|
|
timeout = null;
|
|
return func.apply(this, args);
|
|
};
|
|
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(effect, duration);
|
|
};
|
|
}
|
|
|
|
export function throttle(func, duration) {
|
|
let shouldWait = false;
|
|
|
|
return function (...args) {
|
|
if (!shouldWait) {
|
|
func.apply(this, args);
|
|
shouldWait = true;
|
|
|
|
setTimeout(function () {
|
|
shouldWait = false;
|
|
}, duration);
|
|
}
|
|
};
|
|
}
|