是否可以创建一个节流功能,可以将另一个功能(也有参数)和时间延迟作为参数

时间:2014-08-21 04:17:59

标签: javascript closures throttling

所以我已经编写了一个功能(基于下划线限制),用于不参与参数的功能,但是我想让它足够通用以传入一个参数。具有可变数量参数的函数。这就是我所拥有的:

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

}());

1 个答案:

答案 0 :(得分:9)

throttle(foo(5), 2000)

无效,因为当您使用括号调用函数时,您正在调用该函数。

在将foo传递给限制时,您需要将throttle(function(){ foo(5) }, 2000) 包装在匿名函数中。

x

如果您想跟踪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++; } } 的原始值。在函数中包装foo并返回foo。在闭包范围内捕获该值。

throttle(foo(5), 2000)

然后你实际上可以使用function throttle(func, wait, args){ //do throttle stuff func.apply(null, args); } 因为它在第一次调用时没有执行预期的功能。

此处示例:http://repl.it/XOj/5(已修复示例)

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

throttle(foo(5), 2000)

然后throttle(foo, 2000, [5])变为{{1}}