【问题标题】:Is it possible to create a throttle function that can take in as parameters another function (that also has parameters), and the time delay是否可以创建一个可以将另一个函数(也有参数)作为参数的节流函数,以及时间延迟
【发布时间】:2014-10-14 14:31:15
【问题描述】:

所以我已经为不接受参数的函数编写了一个函数(基于下划线节流),但我想让它足够通用以传入一个可变数量的函数参数。这是我所拥有的:

    (function () {

    var lastTime = new Date().getTime();

    function foo() {
        var newTime = new Date().getTime();
        var gap = newTime - lastTime; // Travels up scope chain to use parents lastTime.  Function has access to variables declared in the same scope
        console.log('foo called,  gap:' + gap);
        lastTime = newTime; // Updates lastTime
        //console.log(x);
        //x++;
    }

    var throttle = function(func, wait) {
        var result;
        var timeout = null; // flag updated through closure
        var previous = 0; // time last run updated through closure

        return function() { //func, wait, timeout, previous available through scope
            var now = new Date().getTime();
            var remaining = wait - (now - previous);

            if (remaining <= 0) {
                clearTimeout(timeout);
                timeout = null;
                previous = now;
                result = func.apply(this, arguments); //func is available through closure
            }
            return result;
        };
    };

    document.addEventListener("scroll", throttle(foo, 1000));
    //document.addEventListener("scroll", throttle(foo(5), 2000));

}());

但我想将 foo 修改为 foo(x) 并让它工作

    (function () {

    var lastTime = new Date().getTime();

    function foo(x) {
        var newTime = new Date().getTime();
        var gap = newTime - lastTime; // Travels up scope chain to use parents lastTime.  Function has access to variables declared in the same scope
        console.log('foo called,  gap:' + gap);
        lastTime = newTime; // Updates lastTime
        console.log(x);
        x++;
    }

    var throttle = function(func, wait) {
        var result;
        var timeout = null; // flag updated through closure
        var previous = 0; // time last run updated through closure

        return function() { //func, wait, timeout, previous available through scope
            var now = new Date().getTime();
            var remaining = wait - (now - previous);

            if (remaining <= 0) {
                clearTimeout(timeout);
                timeout = null;
                previous = now;
                result = func.apply(this, arguments); //func is available through closure
            }
            return result;
        };
    };

    document.addEventListener("scroll", throttle(foo(5), 2000));

}());

【问题讨论】:

  • 你很难确定两者之间的区别。
  • 节流 foo() 和节流 foo(x) 之间的区别
  • 这并不能真正描述您正在寻找的行为。您要保留 x 的初始值吗?您是否每次都想要不同的 x。更不用说你发布了很多代码,有非常微妙的变化。如果不是 addEventListener 行,我不会猜到你想要什么。但这仍然是一个猜测。
  • foo(x) 应该像任何其他 javascript 函数一样运行...请记住限制的用途...我有一个经常调用的函数...我不希望它尽可能多地被调用....第一次调用是立即的,第二次调用是在某个时间段之后才传入的,第三次调用也是在某个时间段之后,等等。现在我不想改变我的功能工作......我希望它在限制之前的工作方式与实施限制之后的工作方式相同。节流的重点当然是放弃线程,让其他事情在函数再次被调用之前发生。

标签: javascript closures throttling


【解决方案1】:

throttle(foo(5), 2000)

将不起作用,因为当您使用括号调用函数时,您正在调用该函数。

在将 foo 传递给油门时,您需要将其包装在匿名函数中。

throttle(function(){
    foo(5)
}, 2000)

如果您想跟踪 x 的原始值。将 foo 包装在一个函数中并返回 foo。将值捕获在闭包范围内。

function foo(x) {
  return function(){
    var newTime = new Date().getTime();
    var gap = newTime - lastTime; // Travels up scope chain to use parents lastTime.  Function has access to variables declared in the same scope
    console.log('foo called,  gap:' + gap);
    lastTime = newTime; // Updates lastTime
    console.log(x);
    x++;
  }        
}

那么你实际上可以给我们throttle(foo(5), 2000),因为它不会在第一次调用时执行预期的功能。

此处示例:http://repl.it/XOj/5(固定示例)

采用任意数量的参数的解决方案,修改油门:

function throttle(func, wait, args){
    //do throttle stuff 
    func.apply(null, args);
}

那么throttle(foo(5), 2000)变成throttle(foo, 2000, [5])

【讨论】:

  • 我在传入之前尝试将 foo(x) 包装在一个匿名函数中,但这没有用,但我会用 foo 包装器检查你的示例,看看它在我标记之前是否有效它作为回答......
  • @StanQuinn 你觉得怎么样?
  • 感谢您的回答! throttle(foo, 2000, [5]) 按预期工作,但 Lodash 文档没有反映这一点...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-04
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2014-12-12
相关资源
最近更新 更多