index.js 754 B

123456789101112131415161718192021222324252627282930
  1. // 防抖 只有在用户停止触发事件一段时间后才执行事件处理函数
  2. export function debounce(fn, delay) {
  3. let timer = null;
  4. return function (...args) {
  5. if (timer) clearTimeout(timer);
  6. timer = setTimeout(() => {
  7. fn.apply(this, args);
  8. }, delay);
  9. };
  10. }
  11. // 节流 确保在一段时间内只执行一次事件处理函数
  12. export function throttle(fn, delay) {
  13. let lastTime = 0; // 记录上一次执行的时间
  14. return function (...args) {
  15. const now = Date.now();
  16. if (now - lastTime >= delay) {
  17. lastTime = now;
  18. fn.apply(this, args);
  19. }
  20. };
  21. }
  22. /**验证手机号格式
  23. * @param {Number} phone 手机号码
  24. */
  25. export function isValidPhone(phone) {
  26. const regex = /^1[3-9]\d{9}$/;
  27. return regex.test(phone.trim());
  28. }