8000 Add `_.throttle` unit test for recursive calls. · lodash/lodash@fbdadec · GitHub
[go: up one dir, main page]

Skip to content

Commit fbdadec

Browse files
committed
Add _.throttle unit test for recursive calls.
Former-commit-id: 7208516b56905c83df73aef6b02cee0101602349
1 parent a068339 commit fbdadec

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

doc/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ _.template('<%= data.hasWith %>', { 'hasWith': 'no' }, { 'variable': 'data' });
22212221
### <a id="_throttlefunc-wait"></a>`_.throttle(func, wait)`
22222222
<a href="#_throttlefunc-wait">#</a> [&#x24C8;](https://github.com/bestiejs/lodash/blob/v0.3.1/lodash.js#L2174 "View in source") [&#x24C9;][1]
22232223
2224-
Creates a new function that, when executed, will only call the `func` 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. Subsequent calls to the throttled function will return the result of the last `func` call.
2224+
Creates a new function that, when executed, will only call the `func` function at most once per every `wait` milliseconds. If the throttled function is invoked more than once during the `wait` timeout, `func` will also be called on the trailing edge of the timeout. Subsequent calls to the throttled function will return the result of the last `func` call.
22252225
22262226
#### Arguments
22272227
1. `func` *(Function)*: The function to throttle.

lodash.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,10 +2155,10 @@
21552155

21562156
/**
21572157
* Creates a new function that, when executed, will only call the `func`
2158-
* function at most once per every `wait` milliseconds. If the throttled function
2159-
* is invoked more than once, `func` will also be called on the trailing edge
2160-
* of the `wait` timeout. Subsequent calls to the throttled function will
2161-
* return the result of the last `func` call.
2158+
* function at most once per every `wait` milliseconds. If the throttled
2159+
* function is invoked more than once during the `wait` timeout, `func` will
2160+
* also be called on the trailing edge of the timeout. Subsequent calls to the
2161+
* throttled function will return the result of the last `func` call.
21622162
*
21632163
* @static
21642164
* @memberOf _

test/test.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,23 @@
642642
}
643643
ok(counter > 1);
644644
});
645+
646+
asyncTest('supports recursive calls', function() {
647+
var counter = 0;
648+
var throttled = _.throttle(function() {
649+
counter++;
650+
if (counter < 4) {
651+
throttled();
652+
}
653+
}, 100);
654+
655+
setTimeout(function() {
656+
ok(counter > 1);
657+
QUnit.start();
658+
}, 220);
659+
660+
throttled();
661+
});
645662
}());
646663

647664
/*--------------------------------------------------------------------------*/
@@ -790,6 +807,7 @@
790807
/*--------------------------------------------------------------------------*/
791808

792809
// explicitly call `QUnit.start()` for Narwhal, Rhino, and RingoJS
793-
QUnit.start();
794-
810+
if (!window.document) {
811+
QUnit.start();
812+
}
795813
}(typeof global == 'object' && global || this));

0 commit comments

Comments
 (0)
0