mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
This commit is contained in:
@@ -90,16 +90,32 @@ $(document).ready(function() {
|
|||||||
_.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
|
_.delay(function(){ ok(deferred, "deferred the function"); start(); }, 50);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("functions: throttle", 1, function() {
|
asyncTest("functions: throttle", 2, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ counter++; };
|
var incr = function(){ counter++; };
|
||||||
var throttledIncr = _.throttle(incr, 100);
|
var throttledIncr = _.throttle(incr, 100);
|
||||||
throttledIncr(); throttledIncr(); throttledIncr();
|
throttledIncr(); throttledIncr(); throttledIncr();
|
||||||
|
setTimeout(throttledIncr, 70);
|
||||||
setTimeout(throttledIncr, 120);
|
setTimeout(throttledIncr, 120);
|
||||||
setTimeout(throttledIncr, 140);
|
setTimeout(throttledIncr, 140);
|
||||||
|
setTimeout(throttledIncr, 190);
|
||||||
setTimeout(throttledIncr, 220);
|
setTimeout(throttledIncr, 220);
|
||||||
setTimeout(throttledIncr, 240);
|
setTimeout(throttledIncr, 240);
|
||||||
_.delay(function(){ ok(counter == 3, "incr was throttled"); start(); }, 400);
|
_.delay(function(){ ok(counter == 1, "incr was called immediately"); }, 30);
|
||||||
|
_.delay(function(){ ok(counter == 4, "incr was throttled"); start(); }, 400);
|
||||||
|
});
|
||||||
|
|
||||||
|
asyncTest("functions: throttle arguments", 2, function() {
|
||||||
|
var value = 0;
|
||||||
|
var update = function(val){ value = val; };
|
||||||
|
var throttledUpdate = _.throttle(update, 100);
|
||||||
|
throttledUpdate(1); throttledUpdate(2); throttledUpdate(3);
|
||||||
|
setTimeout(function(){ throttledUpdate(4); }, 120);
|
||||||
|
setTimeout(function(){ throttledUpdate(5); }, 140);
|
||||||
|
setTimeout(function(){ throttledUpdate(6); }, 260);
|
||||||
|
setTimeout(function(){ throttledUpdate(7); }, 270);
|
||||||
|
_.delay(function(){ ok(value == 1, "updated to latest value"); }, 40);
|
||||||
|
_.delay(function(){ ok(value == 7, "updated to latest value"); start(); }, 400);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("functions: debounce", 1, function() {
|
asyncTest("functions: debounce", 1, function() {
|
||||||
|
|||||||
@@ -520,8 +520,29 @@
|
|||||||
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
return _.delay.apply(_, [func, 1].concat(slice.call(arguments, 1)));
|
||||||
};
|
};
|
||||||
|
|
||||||
// Internal function used to implement `_.throttle` and `_.debounce`.
|
// Returns a function, that, when invoked, will only be triggered at most once
|
||||||
var limit = function(func, wait, debounce) {
|
// during a given window of time.
|
||||||
|
_.throttle = function(func, wait) {
|
||||||
|
var timeout, context, args, throttling, finishThrottle;
|
||||||
|
finishThrottle = _.debounce(function(){ throttling = false; }, wait);
|
||||||
|
return function() {
|
||||||
|
context = this; args = arguments;
|
||||||
|
var throttler = function() {
|
||||||
|
timeout = null;
|
||||||
|
func.apply(context, args);
|
||||||
|
finishThrottle();
|
||||||
|
};
|
||||||
|
if (!timeout) timeout = setTimeout(throttler, wait);
|
||||||
|
if (!throttling) func.apply(context, args);
|
||||||
|
if (finishThrottle) finishThrottle();
|
||||||
|
throttling = true;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// Returns a function, that, as long as it continues to be invoked, will not
|
||||||
|
// be triggered. The function will be called after it stops being called for
|
||||||
|
// N milliseconds.
|
||||||
|
_.debounce = function(func, wait) {
|
||||||
var timeout;
|
var timeout;
|
||||||
return function() {
|
return function() {
|
||||||
var context = this, args = arguments;
|
var context = this, args = arguments;
|
||||||
@@ -529,24 +550,11 @@
|
|||||||
timeout = null;
|
timeout = null;
|
||||||
func.apply(context, args);
|
func.apply(context, args);
|
||||||
};
|
};
|
||||||
if (debounce) clearTimeout(timeout);
|
clearTimeout(timeout);
|
||||||
if (debounce || !timeout) timeout = setTimeout(throttler, wait);
|
timeout = setTimeout(throttler, wait);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
// Returns a function, that, when invoked, will only be triggered at most once
|
|
||||||
// during a given window of time.
|
|
||||||
_.throttle = function(func, wait) {
|
|
||||||
return limit(func, wait, false);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Returns a function, that, as long as it continues to be invoked, will not
|
|
||||||
// be triggered. The function will be called after it stops being called for
|
|
||||||
// N milliseconds.
|
|
||||||
_.debounce = function(func, wait) {
|
|
||||||
return limit(func, wait, true);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Returns a function that will be executed at most one time, no matter how
|
// Returns a function that will be executed at most one time, no matter how
|
||||||
// often you call it. Useful for lazy initialization.
|
// often you call it. Useful for lazy initialization.
|
||||||
_.once = function(func) {
|
_.once = function(func) {
|
||||||
|
|||||||
Reference in New Issue
Block a user