Files
ArtPlayer/packages/artplayer/src/utils/time.js
T
2023-05-13 21:57:45 +08:00

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);
}
};
}