globalFun.js 969 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * 函数防抖
  3. * 触发事件后在n秒后执行,如果n秒内又触发事件,则重新计算时间
  4. */
  5. export function debounce(fn, wait = 1000) {
  6. let timer;
  7. return function () {
  8. let context = this;
  9. let args = arguments;
  10. if (timer) clearTimeout(timer);
  11. timer = setTimeout(() => {
  12. fn.apply(context, args);
  13. }, wait)
  14. }
  15. }
  16. /**
  17. * 函数节流
  18. * 触发事件立即执行,但在n秒内连续触发则不执行
  19. */
  20. export function throttle(fn, wait = 1000) {
  21. let timer;
  22. return function () {
  23. if (timer != null) return;
  24. let context = this;
  25. let args = arguments;
  26. fn.apply(context, args);
  27. timer = setTimeout(() => {
  28. timer = null;
  29. }, wait);
  30. }
  31. }
  32. export function msg(message,dur){
  33. uni.showToast({
  34. icon: 'none',
  35. title: message,
  36. duration: dur || 1500
  37. })
  38. }
  39. export function success(message,dur){
  40. uni.showToast({
  41. icon: 'success',
  42. title: message,
  43. duration: dur || 1500
  44. })
  45. }