util.js 890 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. let _debounceTimeout = null,
  2. _throttleRunning = false
  3. /**
  4. * 防抖
  5. * 参考文章 https://juejin.cn/post/6844903669389885453
  6. *
  7. * @param {Function} 执行函数
  8. * @param {Number} delay 延时ms
  9. */
  10. export const debounce = (fn, delay=500) => {
  11. clearTimeout(_debounceTimeout);
  12. _debounceTimeout = setTimeout(() => {
  13. fn();
  14. }, delay);
  15. }
  16. /**
  17. * 节流
  18. * 参考文章 https://juejin.cn/post/6844903669389885453
  19. *
  20. * @param {Function} 执行函数
  21. * @param {Number} delay 延时ms
  22. */
  23. export const throttle = (fn, delay=500) => {
  24. if(_throttleRunning){
  25. return;
  26. }
  27. _throttleRunning = true;
  28. fn();
  29. setTimeout(() => {
  30. _throttleRunning = false;
  31. }, delay);
  32. }
  33. /**
  34. * toast
  35. */
  36. export const msg = (title = '', param={}) => {
  37. if(!title) return;
  38. uni.showToast({
  39. title,
  40. duration: param.duration || 1500,
  41. mask: param.mask || false,
  42. icon: param.icon || 'none'
  43. });
  44. }