Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Hongbusi opened this issue Jun 20, 2022 · 8 comments
Open

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

Hongbusi opened this issue Jun 20, 2022 · 8 comments
Labels

Comments

@Hongbusi
Copy link
Member

Hongbusi commented Jun 20, 2022

function once(fn) {
  let flag = true
  return function () {
    if (flag) {
      fn()
      flag = false
    }
    return undefined
  }
}
@Hongbusi Hongbusi added handwritten-code JS javascript today 每日一题。 labels Jun 20, 2022
@luckept
Copy link
Member

luckept commented Jun 21, 2022

朴实无华版

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);

@baboon-king
Copy link

const 我是一个只能执行一次的函数 = () => {
  console.log("好可怜,我只能执行一次");
};

const once = (() => {
  let called = false;
  return function (fn) {
    if (!called) {
      called = true;
      return fn();
    }
  };
})();

once(我是一个只能执行一次的函数);
once(我是一个只能执行一次的函数);

image

@baboon-king
Copy link

朴实无华版

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);

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

@Rainer-Yu
Copy link

简单直接

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);

@Hongbusi
Copy link
Member Author

简单直接

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);

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

@chenfan0
Copy link
Member

勉强实现

function once(fn) {
  if (!once.execute) {
    fn()
    once.execute = true
  }
}

const foo = () => console.log('once')
once(foo)
once(foo)

@ChenHaoJie9527
Copy link

ChenHaoJie9527 commented Jun 21, 2022

闭包版

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));

@jp-liu
Copy link

jp-liu commented Jun 21, 2022

function once(fn, maxExec = 1) {
    let onceFn = () => {
        if(maxExec === 0) return
        maxExec--
        fn()
    }
    return onceFn
}
const a = once(() => console.log(1324), 2)

@Hongbusi Hongbusi removed the today 每日一题。 label Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

7 participants