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

Commit fe6d7f7

Browse files
committed
feat($interval): pass arguments in callback
`setInterval ` allows you to pass arguments into the callback function in the …3rd argument. Since that’s taken I added it to the …5th http://mdn.io/setInterval#Syntax
1 parent a01ce6b commit fe6d7f7

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/ng/interval.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ function $IntervalProvider() {
132132
* </example>
133133
*/
134134
function interval(fn, delay, count, invokeApply) {
135-
var setInterval = $window.setInterval,
135+
var args = arguments.length > 4 ? sliceArgs(arguments, 4) : [],
136+
setInterval = $window.setInterval,
136137
clearInterval = $window.clearInterval,
137138
iteration = 0,
138139
skipApply = (isDefined(invokeApply) && !invokeApply),
@@ -141,7 +142,9 @@ function $IntervalProvider() {
141142

142143
count = isDefined(count) ? count : 0;
143144

144-
promise.then(null, null, fn);
145+
promise.then(null, null, function() {
146+
fn.apply(null, args);
147+
});
145148

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

test/ng/intervalSpec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,23 @@ 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, 1, true, 'Task1');
150+
$interval(task2, 1000, 1, true, 'Task2');
151+
$interval(task3, 1000, 1, true, 'I', 'am', 'a', 'Task3', 'spy');
152+
153+
$window.flush(1000);
154+
expect(task1).toHaveBeenCalledWith('Task1');
155+
$window.flush(1000);
156+
expect(task2).toHaveBeenCalledWith('Task2');
157+
$window.flush(1000);
158+
expect(task3).toHaveBeenCalledWith('I', 'am', 'a', 'Task3', 'spy');
159+
}));
160+
161+
145162
it('should return a promise which will be updated with the count on each iteration',
146163
inject(function($interval, $window) {
147164
var log = [],

0 commit comments

Comments
 (0)