Skip to content

实现一个 once 函数, 传入函数参数只执行一次 #17

Open

Description

@Hongbusi
function once(fn) {
  let flag = true
  return function () {
    if (flag) {
      fn()
      flag = false
    }
    return undefined
  }
}

Activity

  1. luckept commented on Jun 21, 2022

    @luckept
    Member

    朴实无华版

    function once(fn) {
        let _window = window;
        if (!_window.onceSet) {
          _window.onceSet = new Set();
        }
        if (!_window.onceSet.has(fn)) {
          fn();
          _window.onceSet.add(fn);
        }
    }
    
    const fn = () => {
        console.log("hi");
    };
    once(fn);
    once(fn);
  2. baboon-king commented on Jun 21, 2022

    @baboon-king
    const 我是一个只能执行一次的函数 = () => {
      console.log("好可怜,我只能执行一次");
    };
    
    const once = (() => {
      let called = false;
      return function (fn) {
        if (!called) {
          called = true;
          return fn();
        }
      };
    })();
    
    once(我是一个只能执行一次的函数);
    once(我是一个只能执行一次的函数);

    image

  3. baboon-king commented on Jun 21, 2022

    @baboon-king

    朴实无华版

    function once(fn) {
        let _window = window;
        if (!_window.onceSet) {
          _window.onceSet = new Set();
        }
        if (!_window.onceSet.has(fn)) {
          fn();
          _window.onceSet.add(fn);
        }
    }
    
    const fn = () => {
        console.log("hi");
    };
    once(fn);
    once(fn);

    卧槽卧槽!简直神之一手啊!

  4. Rainer-Yu commented on Jun 21, 2022

    @Rainer-Yu

    简单直接

    const onceSet = new Set();
    const once = (fn) => {
        if (!onceSet.has(fn) && onceSet.add(fn)) fn?.()
    };
    
    const fn = () => {
        console.log("hi");
    };
    once(fn);
    once(fn);
    once(fn);
  5. Hongbusi commented on Jun 21, 2022

    @Hongbusi
    MemberAuthor

    简单直接

    const onceSet = new Set();
    const once = (fn) => {
        if (!onceSet.has(fn) && onceSet.add(fn)) fn?.()
    };
    
    const fn = () => {
        console.log("hi");
    };
    once(fn);
    once(fn);
    once(fn);

    可以实现,但是多了一个污染变量,还可以优化~

  6. chenfan0 commented on Jun 21, 2022

    @chenfan0
    Member

    勉强实现

    function once(fn) {
      if (!once.execute) {
        fn()
        once.execute = true
      }
    }
    
    const foo = () => console.log('once')
    once(foo)
    once(foo)
  7. ChenHaoJie9527 commented on Jun 21, 2022

    @ChenHaoJie9527

    闭包版

    function before(n, func) {
      let result
      if (typeof func !== 'function') {
        throw new TypeError('Expected a function')
      }
      return function(...args) {
        if (--n > 0) {
          result = func.apply(this, args)
        }
        if (n <= 1) {
          func = undefined
        }
        console.log('result', result);
        return result
      }
    }
    function once(func) {
      return before(2, func)
    }
    const res = once((arg) => arg);
    console.log('once', res(10));
    console.log('once', res(2));
  8. jp-liu commented on Jun 21, 2022

    @jp-liu
    function once(fn, maxExec = 1) {
        let onceFn = () => {
            if(maxExec === 0) return
            maxExec--
            fn()
        }
        return onceFn
    }
    const a = once(() => console.log(1324), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions