mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-24 06:31:20 -08:00
分离工具函数
This commit is contained in:
1 parent
7a84c262da
commit
8ef582f714
22 files changed
+724
-687
No files matched your search
@@ -0,0 +1,46 @@
|
||||
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;
|
||||
}
|
||||
Reference in new issue
Block a user