mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-14 08:26:16 -08:00
47 lines
1.0 KiB
JavaScript
47 lines
1.0 KiB
JavaScript
export function sleep(ms = 0) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
export function debounce(func, wait, context) {
|
|
let timeout;
|
|
function fn(...args) {
|
|
const later = function later() {
|
|
timeout = null;
|
|
func.apply(context, args);
|
|
};
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(later, wait);
|
|
}
|
|
|
|
fn.clearTimeout = function ct() {
|
|
clearTimeout(timeout);
|
|
};
|
|
|
|
return fn;
|
|
}
|
|
|
|
export function throttle(callback, delay) {
|
|
let isThrottled = false;
|
|
let args;
|
|
let context;
|
|
function fn(...args2) {
|
|
if (isThrottled) {
|
|
args = args2;
|
|
context = this;
|
|
return;
|
|
}
|
|
|
|
isThrottled = true;
|
|
callback.apply(this, args2);
|
|
setTimeout(() => {
|
|
isThrottled = false;
|
|
if (args) {
|
|
fn.apply(context, args);
|
|
args = null;
|
|
context = null;
|
|
}
|
|
}, delay);
|
|
}
|
|
return fn;
|
|
}
|