Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4f1f9cf

Browse files
PatrickJSpetebacondarwin
authored andcommitted
feat($interval): pass additional arguments to the callback
Similar to how [`setInterval`](http://mdn.io/setInterval#Syntax) works, this commit allows users of `$interval` to add additional parameters to the call, which will now be passed on to the callback function. Closes #10632
1 parent 3a4b6b8 commit 4f1f9cf

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/ng/interval.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function $IntervalProvider() {
3939
* indefinitely.
4040
* @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
4141
* will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
42+
* @param {...*=} Pass additional parameters to the executed function.
4243
* @returns {promise} A promise which will be notified on each iteration.
4344
*
4445
* @example
@@ -132,7 +133,9 @@ function $IntervalProvider() {
132133
* </example>
133134
*/
134135
function interval(fn, delay, count, invokeApply) {
135-
var setInterval = $window.setInterval,
136+
var hasParams = arguments.length > 4,
137+
args = hasParams ? sliceArgs(arguments, 4) : [],
138+
setInterval = $window.setInterval,
136139
clearInterval = $window.clearInterval,
137140
iteration = 0,
138141
skipApply = (isDefined(invokeApply) && !invokeApply),
@@ -141,7 +144,9 @@ function $IntervalProvider() {
141144

142145
count = isDefined(count) ? count : 0;
143146

144-
promise.then(null, null, fn);
147+
promise.then(null, null, (!hasParams) ? fn : function() {
148+
fn.apply(null, args);
149+
});
145150

146151
promise.$$intervalId = setInterval(function tick() {
147152
deferred.notify(iteration++);

src/ngMock/angular-mocks.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ angular.mock.$LogProvider = function() {
425425
* indefinitely.
426426
* @param {boolean=} [invokeApply=true] If set to `false` skips model dirty checking, otherwise
427427
* will invoke `fn` within the {@link ng.$rootScope.Scope#$apply $apply} block.
428+
* @param {...*=} Pass additional parameters to the executed function.
428429
* @returns {promise} A promise which will be notified on each iteration.
429430
*/
430431
angular.mock.$IntervalProvider = function() {
@@ -435,13 +436,17 @@ angular.mock.$IntervalProvider = function() {
435436
now = 0;
436437

437438
var $interval = function(fn, delay, count, invokeApply) {
438-
var iteration = 0,
439+
var hasParams = arguments.length > 4,
440+
args = hasParams ? Array.prototype.slice.call(arguments, 4) : [],
441+
iteration = 0,
439442
skipApply = (angular.isDefined(invokeApply) && !invokeApply),
440443
deferred = (skipApply ? $$q : $q).defer(),
441444
promise = deferred.promise;
442445

443446
count = (angular.isDefined(count)) ? count : 0;
444-
promise.then(null, null, fn);
447+
promise.then(null, null, (!hasParams) ? fn : function() {
448+
fn.apply(null, args);
449+
});
445450

446451
promise.$$intervalId = nextRepeatId;
447452

test/ng/intervalSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,31 @@ describe('$interval', function() {
142142
}));
143143

144144

145+
it('should allow you to specify a number of arguments', inject(function($interval, $window) {
146+
var task1 = jasmine.createSpy('task1'),
147+
task2 = jasmine.createSpy('task2'),
148+
task3 = jasmine.createSpy('task3');
149+
$interval(task1, 1000, 2, true, 'Task1');
150+
$interval(task2, 1000, 2, true, 'Task2');
151+
$interval(task3, 1000, 2, true, 'I', 'am', 'a', 'Task3', 'spy');
152+
153+
$window.flush(1000);
154+
expect(task1).toHaveBeenCalledWith('Task1');
155+
expect(task2).toHaveBeenCalledWith('Task2');
156+
expect(task3).toHaveBeenCalledWith('I', 'am', 'a', 'Task3', 'spy');
157+
158+
task1.reset();
159+
task2.reset();
160+
task3.reset();
161+
162+
$window.flush(1000);
163+
expect(task1).toHaveBeenCalledWith('Task1');
164+
expect(task2).toHaveBeenCalledWith('Task2');
165+
expect(task3).toHaveBeenCalledWith('I', 'am', 'a', 'Task3', 'spy');
166+
167+
}));
168+
169+
145170
it('should return a promise which will be updated with the count on each iteration',
146171
inject(function($interval, $window) {
147172
var log = [],

0 commit comments

Comments
 (0)