mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
lodash: Optimize and simplify throttle. [cowboy, jddalton]
Former-commit-id: 52e19aeb8671e86a13bf54876bf1f3f1b4644437
This commit is contained in:
40
lodash.js
40
lodash.js
@@ -1701,8 +1701,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new function that, when executed, will only call the original
|
* Creates a new function that, when executed, will only call the `func`
|
||||||
* function at most once per every `wait` milliseconds.
|
* function at most once per every `wait` milliseconds. If the throttled function
|
||||||
|
* is invoked more than once, `func` will also be called on the trailing edge
|
||||||
|
* of the `wait` timeout.
|
||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
@@ -1716,32 +1718,32 @@
|
|||||||
* jQuery(window).on('scroll', throttled);
|
* jQuery(window).on('scroll', throttled);
|
||||||
*/
|
*/
|
||||||
function throttle(func, wait) {
|
function throttle(func, wait) {
|
||||||
var args, more, thisArg, throttling, timeout,
|
var args,
|
||||||
whenDone = debounce(function() { more = throttling = false; }, wait);
|
result,
|
||||||
|
thisArg,
|
||||||
|
timeoutId,
|
||||||
|
lastCalled = 0;
|
||||||
|
|
||||||
function delayed() {
|
function trailingCall() {
|
||||||
timeout = undefined;
|
lastCalled = new Date;
|
||||||
if (more) {
|
timeoutId = undefined;
|
||||||
func.apply(thisArg, args);
|
func.apply(thisArg, args);
|
||||||
}
|
|
||||||
whenDone();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return function() {
|
return function() {
|
||||||
var result;
|
var now = new Date,
|
||||||
|
remain = wait - (now - lastCalled);
|
||||||
|
|
||||||
args = arguments;
|
args = arguments;
|
||||||
thisArg = this;
|
thisArg = this;
|
||||||
|
|
||||||
if (!timeout) {
|
if (remain <= 0) {
|
||||||
timeout = setTimeout(delayed, wait);
|
lastCalled = now;
|
||||||
}
|
|
||||||
if (throttling) {
|
|
||||||
more = true;
|
|
||||||
} else {
|
|
||||||
throttling = true;
|
|
||||||
result = func.apply(thisArg, args);
|
result = func.apply(thisArg, args);
|
||||||
}
|
}
|
||||||
whenDone();
|
else if (!timeoutId) {
|
||||||
|
timeoutId = setTimeout(trailingCall, remain);
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
25
test/test.js
25
test/test.js
@@ -187,6 +187,31 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.throttle');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('subsequent calls should return the result of the first call', function() {
|
||||||
|
var throttled = _.throttle(function(value) { return value; }, 100),
|
||||||
|
result = [throttled('x'), throttled('y')];
|
||||||
|
|
||||||
|
deepEqual(result, ['x', 'x']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('supports calls in a loop', function() {
|
||||||
|
var counter = 0,
|
||||||
|
throttled = _.throttle(function() { counter++; }, 100),
|
||||||
|
start = new Date,
|
||||||
|
limit = 220;
|
||||||
|
|
||||||
|
while ((new Date - start) < limit) {
|
||||||
|
throttled();
|
||||||
|
}
|
||||||
|
equal(counter, 3);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.toArray');
|
QUnit.module('lodash.toArray');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user