You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.5 KiB
107 lines
2.5 KiB
import Taro from '@tarojs/taro'
|
|
|
|
export const sleep = async (longTime) => new Promise((resolve) => setTimeout(resolve, longTime))
|
|
|
|
/**
|
|
* 函数防抖 (立即执行版)
|
|
* @param {function} fn 函数
|
|
* @param {number} delay 延迟执行毫秒数
|
|
*/
|
|
export const debounceStart = (fn, delay = 2000) => debounce(fn,delay,true);
|
|
|
|
|
|
/**
|
|
* 函数防抖 (非立即执行版)
|
|
* @param {function} fn 函数
|
|
* @param {number} delay 延迟执行毫秒数
|
|
*/
|
|
export const debounceEnd = (fn, delay = 2000) => debounce(fn,delay,false);
|
|
|
|
|
|
/**
|
|
* 函数防抖 (完全版)
|
|
* @param {function} fn 函数
|
|
* @param {number} delay 延迟执行毫秒数
|
|
* @param {boolean} immediate true 表立即执行,false 表非立即执行
|
|
*/
|
|
export const debounce = (fn, delay, immediate = false) => {
|
|
let timer = null;
|
|
let status = true;
|
|
if (!immediate) return function () {
|
|
let args = arguments;
|
|
if (timer) clearTimeout(timer);
|
|
timer = setTimeout(() => fn.apply(this, args), delay);
|
|
};
|
|
else return function () {
|
|
clearTimeout(timer);
|
|
if (status) {
|
|
status = false;
|
|
fn.call(this, arguments);
|
|
}
|
|
timer = setTimeout(() => status = true, delay);
|
|
};
|
|
};
|
|
|
|
|
|
/**
|
|
* 函数节流 延迟执行
|
|
* @param {function} fn 函数
|
|
* @param {number} delay 延迟执行毫秒数
|
|
*/
|
|
export const throttle = (fn, delay = 2000) => {
|
|
let timer = null;
|
|
return function () {
|
|
let args = arguments;
|
|
if (!timer) {
|
|
timer = setTimeout(() => {
|
|
timer = null;
|
|
fn.apply(this, args);
|
|
}, delay);
|
|
}
|
|
|
|
};
|
|
};
|
|
//防止多次重复点击 (函数节流)
|
|
export const throttleClick = (fn, gapTime) => {
|
|
if (gapTime == null || gapTime == undefined) {
|
|
gapTime = 1500;
|
|
}
|
|
let _lastTime = null;
|
|
return function () {
|
|
let _nowTime = + new Date();
|
|
if (_nowTime - _lastTime > gapTime || !_lastTime) {
|
|
fn();
|
|
_lastTime = _nowTime;
|
|
}
|
|
};
|
|
};
|
|
|
|
/**
|
|
* 显示一个轻提示
|
|
* @param {string | object} msg 显示内容
|
|
*/
|
|
export const toast = msg => {
|
|
Taro.showToast({
|
|
title: typeof msg === 'object' ? JSON.stringify(msg) : msg,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
|
|
/**
|
|
* 执行定时时间后的异步任务
|
|
* @param {number} time 毫秒
|
|
*/
|
|
export const asyncTimeOut = time => {
|
|
let resolveFunc
|
|
let rejectFunc
|
|
const pro = new Promise((resolve, reject) => {
|
|
resolveFunc = resolve
|
|
rejectFunc = reject
|
|
})
|
|
const timer = setTimeout(() => resolveFunc({ code: 200, message: '倒计时结束', type: 'timeout' }), time)
|
|
pro.clear = () => {
|
|
clearTimeout(timer)
|
|
rejectFunc({ code: 500, message: '清除倒计时' })
|
|
}
|
|
return pro
|
|
}
|