| 123456789101112131415161718192021222324252627282930 |
- // 防抖 只有在用户停止触发事件一段时间后才执行事件处理函数
- export function debounce(fn, delay) {
- let timer = null;
- return function (...args) {
- if (timer) clearTimeout(timer);
- timer = setTimeout(() => {
- fn.apply(this, args);
- }, delay);
- };
- }
- // 节流 确保在一段时间内只执行一次事件处理函数
- export function throttle(fn, delay) {
- let lastTime = 0; // 记录上一次执行的时间
- return function (...args) {
- const now = Date.now();
- if (now - lastTime >= delay) {
- lastTime = now;
- fn.apply(this, args);
- }
- };
- }
- /**验证手机号格式
- * @param {Number} phone 手机号码
- */
- export function isValidPhone(phone) {
- const regex = /^1[3-9]\d{9}$/;
- return regex.test(phone.trim());
- }
|