小编Sta*_*inn的帖子

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

因此,我已经编写了一个函数(基于下划线限制),用于不接受参数的函数,但我想使其足够通用,以传递具有可变数量参数的函数.这就是我所拥有的:

    (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 Code Online (Sandbox Code Playgroud)

javascript closures throttling

3
推荐指数
1
解决办法
3419
查看次数

标签 统计

closures ×1

javascript ×1

throttling ×1