// Get information on the width and height of the rectangular box
export function querySelect(selector) {
return new Promise((resolve, reject) => {
const query = wx.createSelectorQuery()
query.select(selector).boundingClientRect()
query.exec((res) => {
resolve(...res)
})
})
}
// throttling
export function ymThrottle(
cb,
interval = 100,
{ leading = true, traling = false } = {}
) {
let startTime = 0;
let timer = null;
const _throttle = function (...args) {
return new Promise((resolve, reject) => {
try {
const nowTime = Date.now();
if (!leading && startTime === 0) {
startTime = nowTime;
}
const waitTime = interval - (nowTime - startTime);
if (waitTime <= 0) {
if (timer) clearTimeout(timer);
const res = cb.apply(this, args);
resolve(res);
startTime = nowTime;
timer = null;
return;
}
// Implement tail cancellation
// Add a timer after the interval point
// If it is a spacing point, the timer will be cancelled
if (traling && !timer) {
timer = setTimeout(() => {
const res = cb.apply(this, args);
resolve(res);
startTime = Date.now();
timer = null;
}, waitTime);
}
} catch (error) {
reject(error);
}
});
};
// Cancel
_throttle.cancel = function () {
if (timer) clearTimeout(timer);
};
return _throttle;
}