Is it possible to create a throttle function that can take in as parameters another function (that also has parameters), and the time delay

Stan Quinn

So I've already written a function that works (based on underscores throttle) for functions that don't take in a parameter, but I'd like to make it generic enough to pass in a function with a variable number of parameters. Here's what I have:

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

}());

But I'd like to modify foo to foo(x) and get this to work

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

}());
Gabs00

throttle(foo(5), 2000)

Will not work because when you call a function with the parenthesis, you are invoking the function.

You would need to wrap foo in an anonymous function when passing it to throttle.

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

If you wanted to keep track of the original value of x. wrap foo in a function and return foo. trapping the value in closure scope.

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++;
  }        
}

Then you actually can us throttle(foo(5), 2000) because it does not execute the intended function on the first call.

Example here: http://repl.it/XOj/5 (fixed example)

A solution that takes in any number of args, modifies throttle:

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

Then throttle(foo(5), 2000) becomes throttle(foo, 2000, [5])

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

A delegate for a function with variable parameters

来自分类Dev

Can parameters from a parametrized class be used in external function definitions?

来自分类Dev

Best practice for unused function parameters

来自分类Dev

Statsmodels OLS function for multiple regression parameters

来自分类Dev

Can ExpressMapper take Interfaces in the Register function?

来自分类Dev

qsort: Cast the comparator function itself or the parameters in the body of comparator function?

来自分类Dev

How can I preserve the value of my parameters through a function so it can be used multiple times with its initial value?

来自分类Dev

How to pass unlimited arguments and one or two parameters to a JavaScript function?

来自分类Dev

Why this code passing switch parameters to a PowerShell function fails

来自分类Dev

Friend function that takes 2 classes in parameters - 'Class' not defined

来自分类Dev

subs 或 function-parameters 中的问号是什么意思?

来自分类Dev

Python: numba, how can constructor take a function as an argument?

来自分类Dev

check if expression has free parameters

来自分类Dev

Is it possible to get POST parameters from REST request?

来自分类Dev

Function that extend another function with prototype

来自分类Dev

How do you use #call directive for Red function with string datatypes as parameters?

来自分类Dev

Dart / Flutter混淆,“对参数使用通用函数类型语法”(use_function_type_syntax_for_parameters)

来自分类Dev

Is it possible to 'read' a function in Haskell?

来自分类Dev

How can I create a php function that will gather all

来自分类Dev

Time a function in C++

来自分类Dev

transfer function in time domain

来自分类Dev

Can I Access Parameters of a Computation Expression?

来自分类Dev

CREATE AGGREGATE FUNCTION的特权

来自分类Dev

Is there any way to call a function by another function argument?

来自分类Dev

function foo that has the type ’a * ’a -> int

来自分类Dev

Runtime function branching on compile-time function?

来自分类Dev

Jquery delay is inconsistent while using inside a setInterval function

来自分类Dev

使用setTimeout(function,delay)的函数之间的JavaScript延迟

来自分类Dev

Referencing another function in array of functions

Related 相关文章

  1. 1

    A delegate for a function with variable parameters

  2. 2

    Can parameters from a parametrized class be used in external function definitions?

  3. 3

    Best practice for unused function parameters

  4. 4

    Statsmodels OLS function for multiple regression parameters

  5. 5

    Can ExpressMapper take Interfaces in the Register function?

  6. 6

    qsort: Cast the comparator function itself or the parameters in the body of comparator function?

  7. 7

    How can I preserve the value of my parameters through a function so it can be used multiple times with its initial value?

  8. 8

    How to pass unlimited arguments and one or two parameters to a JavaScript function?

  9. 9

    Why this code passing switch parameters to a PowerShell function fails

  10. 10

    Friend function that takes 2 classes in parameters - 'Class' not defined

  11. 11

    subs 或 function-parameters 中的问号是什么意思?

  12. 12

    Python: numba, how can constructor take a function as an argument?

  13. 13

    check if expression has free parameters

  14. 14

    Is it possible to get POST parameters from REST request?

  15. 15

    Function that extend another function with prototype

  16. 16

    How do you use #call directive for Red function with string datatypes as parameters?

  17. 17

    Dart / Flutter混淆,“对参数使用通用函数类型语法”(use_function_type_syntax_for_parameters)

  18. 18

    Is it possible to 'read' a function in Haskell?

  19. 19

    How can I create a php function that will gather all

  20. 20

    Time a function in C++

  21. 21

    transfer function in time domain

  22. 22

    Can I Access Parameters of a Computation Expression?

  23. 23

    CREATE AGGREGATE FUNCTION的特权

  24. 24

    Is there any way to call a function by another function argument?

  25. 25

    function foo that has the type ’a * ’a -> int

  26. 26

    Runtime function branching on compile-time function?

  27. 27

    Jquery delay is inconsistent while using inside a setInterval function

  28. 28

    使用setTimeout(function,delay)的函数之间的JavaScript延迟

  29. 29

    Referencing another function in array of functions

热门标签

归档