StarFire_xm
  • 文章
  • 粉丝
  • 评论

闭包对某个功能方法确保只执行一次

2025-08-07 17:19:290 次浏览0 次评论技能类型: js
//每次只执行一次
const showModulBounce = (() => {
  let hasExecuted = false; // 添加标志位判断是否已执行
  return () => {
      if (!hasExecuted) {
          hasExecuted = true;
          showModal();
      }
  };
  function showModal() {
    uni.showModal({
      title: 'xxx',
      content: 'xxx',
      showCancel: false,
      success: (res) => {
        if (res.confirm) {
            
        }
        // 重置标志位,允许下次立即执行
        hasExecuted = false;
      }
    });
  }
})();


    发表

    还没有评论哦,来抢个沙发吧!