TomVista
V2EX  ›  问与答

js 函数闭包和柯里化后怎么传参给 return 的函数;

  •  
  •   TomVista · May 14, 2019 · 1406 views
    This topic created in 2581 days ago, the information mentioned may be changed or developed.
    /**
     * 返回一个函数,这个函数执行(等于执行 f)后的 time 毫秒时间内,再次被调用不会执行 f
     * @param {()=>any} f 
     * @param {number} time 
     * @returns {()=>any}
     */
    function pulseFunction(f, time) {
      var able = true;
      return function () {
        if (able) {
          able = false;
          f();
          setTimeout(() => {
            able = true;
          }, time);
        }
      };
    }
    
    var submit = pulseFunction(_submit,500);
    submit(...args); //多个 arg 怎么传给_submit; 还是传个对象. 另外这里的 jsdoc 怎么写
    
    3 replies    2019-05-14 16:23:34 +08:00
    yigedala
        1
    yigedala  
       May 14, 2019
    function pulseFunction(f, time) {
    var able = true;
    return function () {
    if (able) {
    able = false;
    f.apply(this, arguments)
    setTimeout(() => {
    able = true;
    }, time);
    }
    };
    }
    TomVista
        2
    TomVista  
    OP
       May 14, 2019
    @pierswu 明白了,谢谢. 头像萌翻了.
    yigedala
        3
    yigedala  
       May 14, 2019
    @TomVista 👍
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2677 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 10:54 · PVG 18:54 · LAX 03:54 · JFK 06:54
    ♥ Do have faith in what you're doing.