Files
ArtPlayer/packages/artplayer/src/utils/time.js
T
2019-11-14 11:24:02 +08:00

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