Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b49404e

Browse files
committedJan 4, 2015
feat($timeout): pass arguments in callback
setTimeout allows you to pass arguments into the callback function in the …3rd argument. Since that’s taken I added it to the …4th mdn.io/setTimeout#Syntax
1 parent a01ce6b commit b49404e

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed
 

‎src/ng/timeout.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,21 @@ function $TimeoutProvider() {
2828
* @param {number=} [delay=0] Delay in milliseconds.
2929
* @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
3030
* will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
31+
* @param {...*=} Pass additional parameters to the executed function.
3132
* @returns {Promise} Promise that will be resolved when the timeout is reached. The value this
3233
* promise will be resolved with is the return value of the `fn` function.
3334
*
3435
*/
3536
function timeout(fn, delay, invokeApply) {
36-
var skipApply = (isDefined(invokeApply) && !invokeApply),
37+
var args = arguments.length > 3 ? sliceArgs(arguments, 3) : [],
38+
skipApply = (isDefined(invokeApply) && !invokeApply),
3739
deferred = (skipApply ? $$q : $q).defer(),
3840
promise = deferred.promise,
3941
timeoutId;
4042

4143
timeoutId = $browser.defer(function() {
4244
try {
43-
deferred.resolve(fn());
45+
deferred.resolve(fn.apply(null, args));
4446
} catch (e) {
4547
deferred.reject(e);
4648
$exceptionHandler(e);

‎test/ng/timeoutSpec.js

+13
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ describe('$timeout', function() {
145145
}));
146146

147147

148+
it('should pass the timeout arguments in the timeout callback',
149+
inject(function($timeout, log) {
150+
var promise1 = $timeout(function(arg) { throw arg; }, 0, true, 'Some Argument');
151+
var promise2 = $timeout(function(arg1, args2) { throw arg1 +' '+ args2; }, 0, true, 'Are Meant', 'To Be Thrown');
152+
153+
promise1.then(log.fn('success'), function(reason) { log('error: ' + reason); });
154+
promise2.then(log.fn('success'), function(reason) { log('error: ' + reason); });
155+
$timeout.flush();
156+
157+
expect(log).toEqual('error: Some Argument; error: Are Meant To Be Thrown');
158+
}));
159+
160+
148161
it('should forget references to relevant deferred even when exception is thrown',
149162
inject(function($timeout, $browser) {
150163
// $browser.defer.cancel is only called on cancel if the deferred object is still referenced

0 commit comments

Comments
 (0)
This repository has been archived.