ViewUtils.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 界面相关的工具
  3. */
  4. module.exports = {
  5. /**
  6. * 防抖动。仅编辑器中编辑时用到,延后执行某函数,避免频繁刷新UI
  7. *
  8. * @author Pyden
  9. * @date 2019-03-21
  10. * @param {function} func 延后执行的函数
  11. * @param {int} wait 延后时长(毫秒)
  12. * @param {boolean} immediate 是否立刻执行。默认否
  13. * @returns {function} 新封装的函数
  14. */
  15. debounce (func, wait, immediate) {
  16. let timeout;
  17. return function () {
  18. let context = this, args = arguments;
  19. let later = function () {
  20. timeout = undefined;
  21. if (!immediate) {
  22. func.apply(context, args);
  23. }
  24. };
  25. let callNow = immediate && !timeout;
  26. clearTimeout(timeout);
  27. timeout = setTimeout(later, wait);
  28. if (callNow) {
  29. func.apply(context, args);
  30. }
  31. };
  32. },
  33. /**
  34. * @description 获取网络图片路径
  35. * @author Wetion
  36. * @date 2019-05-21
  37. * @param {String} imgName
  38. * @returns {String}
  39. */
  40. getNetImagePath (imgName) {
  41. if (!window.JMC.RES_ROOT_URL) {
  42. G.LogUtils.error('没有定义 window.JMC.RES_ROOT_URL');
  43. return '';
  44. }
  45. return cc.path.join(window.JMC.RES_ROOT_URL, 'ssmj/res_h5/icon_img/' + imgName);
  46. },
  47. getNode (root, path, component) {
  48. let node = cc.find(path, root);
  49. if (node && component) {
  50. return node.getComponent(component);
  51. }
  52. return node;
  53. }
  54. };