mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Add flush to debounced methods.
This commit is contained in:
17
lodash.js
17
lodash.js
@@ -7904,10 +7904,10 @@
|
|||||||
* Creates a debounced function that delays invoking `func` until after `wait`
|
* Creates a debounced function that delays invoking `func` until after `wait`
|
||||||
* milliseconds have elapsed since the last time the debounced function was
|
* milliseconds have elapsed since the last time the debounced function was
|
||||||
* invoked. The debounced function comes with a `cancel` method to cancel
|
* invoked. The debounced function comes with a `cancel` method to cancel
|
||||||
* delayed invocations. Provide an options object to indicate that `func`
|
* delayed `func` invocations and a `flush` method to immediately invoke them.
|
||||||
* should be invoked on the leading and/or trailing edge of the `wait` timeout.
|
* Provide an options object to indicate that `func` should be invoked on the
|
||||||
* Subsequent calls to the debounced function return the result of the last
|
* leading and/or trailing edge of the `wait` timeout. Subsequent calls to the
|
||||||
* `func` invocation.
|
* debounced function return the result of the last `func` invocation.
|
||||||
*
|
*
|
||||||
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
|
* **Note:** If `leading` and `trailing` options are `true`, `func` is invoked
|
||||||
* on the trailing edge of the timeout only if the the debounced function is
|
* on the trailing edge of the timeout only if the the debounced function is
|
||||||
@@ -8020,6 +8020,14 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function flush() {
|
||||||
|
if ((timeoutId && trailingCall) || (maxTimeoutId && trailing)) {
|
||||||
|
result = func.apply(thisArg, args);
|
||||||
|
}
|
||||||
|
cancel();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
function maxDelayed() {
|
function maxDelayed() {
|
||||||
complete(trailing, timeoutId);
|
complete(trailing, timeoutId);
|
||||||
}
|
}
|
||||||
@@ -8066,6 +8074,7 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
debounced.cancel = cancel;
|
debounced.cancel = cancel;
|
||||||
|
debounced.flush = flush;
|
||||||
return debounced;
|
return debounced;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
test/test.js
27
test/test.js
@@ -18732,6 +18732,33 @@
|
|||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
QUnit.test('_.' + methodName + ' should support flushing delayed calls', function(assert) {
|
||||||
|
assert.expect(2);
|
||||||
|
|
||||||
|
var done = assert.async();
|
||||||
|
|
||||||
|
if (!(isRhino && isModularize)) {
|
||||||
|
var callCount = 0;
|
||||||
|
|
||||||
|
var funced = func(function() {
|
||||||
|
return ++callCount;
|
||||||
|
}, 32, { 'leading': false });
|
||||||
|
|
||||||
|
funced();
|
||||||
|
var actual = funced.flush();
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
assert.strictEqual(actual, 1);
|
||||||
|
assert.strictEqual(callCount, 1);
|
||||||
|
done();
|
||||||
|
}, 64);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest(assert, 2);
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user