common.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. export function debounce(func, wait = 3000, immediate = true) {
  2. let timeout;
  3. return function () {
  4. console.log(11112, timeout);
  5. const context = this;
  6. const args = [...arguments];
  7. if (timeout) clearTimeout(timeout);
  8. if (immediate) {
  9. const callNow = !timeout;
  10. timeout = setTimeout(() => {
  11. timeout = null;
  12. }, wait);
  13. if (callNow) func.apply(context, args);
  14. } else {
  15. timeout = setTimeout(() => {
  16. func.apply(context, args);
  17. }, wait);
  18. }
  19. };
  20. }
  21. export function throttle(fn, delay = 1000) {
  22. let lastTime = 0;
  23. return function () {
  24. let _this = this;
  25. let _arguments = arguments;
  26. let now = new Date().getTime();
  27. if (now - lastTime > delay) {
  28. fn.apply(_this, _arguments);
  29. lastTime = now;
  30. }
  31. };
  32. }
  33. export function reload(option = {}) {
  34. // #ifdef H5
  35. location.reload();
  36. // #endif
  37. // #ifdef MP-WEIXIN
  38. const pages = getCurrentPages();
  39. const perpage = pages[pages.length - 1];
  40. perpage.onLoad(option);
  41. // #endif
  42. }