1. 方法封装
let moment = require('dayjs')
const formatTime = (value = new Date(), type = "YYYY-MM-DD HH:mm:ss") => {
return moment(value).format(type);
};
const chronologicalOrder = (targetTime = '2022-08-25 19:36:16', sourceTime = new Date()) => {
return moment(targetTime).isAfter(moment(sourceTime))
}
const debounce = (cb, delay = 1000) => {
let timer = null;
return function () {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
cb.apply(this, arguments);
}, delay);
};
};
const throttle = (cb, delay = 1000) => {
let lastTime = 0;
return function () {
let _this = this;
let _arguments = arguments;
let now = new Date().getTime();
if (now - lastTime > delay) {
cb.apply(_this, _arguments);
lastTime = now;
}
};
};
export {
formatTime,
chronologicalOrder,
debounce,
throttle
};
2. app.js 引入挂载
let {
formatTime,
chronologicalOrder,
debounce,
throttle
} = require('./utils/index.js')
App({
onLaunch() {},
formatTime,
chronologicalOrder,
debounce,
throttle
})
3. 具体使用
const app = getApp()
Page({
lower: app.throttle(function () {
}, 1000),
formatTimeFunc(){
app.formatTime(new Date());
app.formatTime(new Date(), 'YYYY年MM月DD日');
},
isAfterThe specifiedDate(){
app.chronologicalOrder('2039.12.24');
app.chronologicalOrder('2039.12.24', '2022.9.29');
},
})