added _.defer definition, fixed bug in _.debounce tests

This commit is contained in:
Brian Zengel
2013-10-18 23:35:49 -04:00
parent de27ae7af2
commit 827460a00b
2 changed files with 15 additions and 22 deletions
+3 -10
View File
@@ -451,7 +451,7 @@ curried(1)(2)(3);
curried(1, 2)(3);
curried(1, 2, 3);
var lazyLayout: Function = _.debounce(calculateLayout, 150);
var lazyLayout: Function = _.debounce(function() {}, 150);
jQuery(window).on('resize', lazyLayout);
jQuery('#postbox').on('click', <Function>_.debounce(function() {}, 300, {
@@ -464,6 +464,8 @@ source.addEventListener('message', <Function>_.debounce(function() {}, 250, {
'maxWait': 1000
}), false);
result = <number>_.defer(function() { console.log('deferred'); });
////////////////////////////////////////////////////////////////////////////////////////
//WHAT'S LEFT
@@ -494,19 +496,10 @@ var fibonacci = _.memoize(function (n) {
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
});
var log = _.bind(console.log, console);
_.delay(log, 1000, 'logged later');
_.defer(function () { alert('deferred'); });
var updatePosition = () => alert('updating position...');
var throttled = _.throttle(updatePosition, 100);
$(window).scroll(throttled);
var calculateLayout = () => alert('calculating layout...');
var lazyLayout = _.debounce(calculateLayout, 300);
$(window).resize(lazyLayout);
var createApplication = () => alert('creating application...');
var initialize = _.once(createApplication);
initialize();
+12 -12
View File
@@ -1821,6 +1821,18 @@ declare module _ {
trailing?: boolean;
}
/**
* Defers executing the func function until the current call stack has cleared. Additional
* arguments will be provided to func when it is invoked.
* @param func The function to defer.
* @param args Arguments to invoke the function with.
* @return The timer id.
**/
export function defer(
func: Function,
...args: any[]): number;
@@ -1928,18 +1940,6 @@ declare module _ {
func: Function,
...arguments: any[]): any;
/**
* Defers invoking the function until the current call stack has cleared, similar to using setTimeout
* with a delay of 0. Useful for performing expensive computations or HTML rendering in chunks without
* blocking the UI thread from updating. If you pass the optional arguments, they will be forwarded on
* to the function when it is invoked.
* @param fn The function to defer.
* @param arguments Additional arguments to pass to `fn`.
**/
export function defer(
fn: Function,
...arguments: any[]): void;
/**
* Creates and returns a new, throttled version of the passed function, that, when invoked repeatedly,
* will only actually call the original function at most once per every wait milliseconds. Useful for