Skip to content
This repository

Passing arguments to setInterval and setTimeout callbacks #283

Closed
wants to merge 1 commit into from

3 participants

Mickey Reiss Davis W. Frank bertrand-pivotal
Mickey Reiss

I noticed an issue where Jasmine's mock setInterval and setTimeout functions had different behavior from most browsers (read, non-IE).

In non-IE browsers, you can pass more than two arguments to setInterval. The first two, which are implemented in Jasmine, are the callback and the time in ms. The additional arguments specify the arguments that are supposed to be applied to the callback function; Jasmine does not capture these arguments or pass them to the callback.

setInterval(callback, time, [arg1, arg2, ...])

See the attached code for an example of this usage.

I could imagine that this behavior was not included intentionally in order to avoid Internet Explorer compatibility issues. I am also aware of one basic workaround to this issue that involves wrapping the callback in an anonymous function.

However, if you agree that these specs are correct, reply to this pull request, and I can try to implement the behavior.

Thanks!

See also: http://msdn.microsoft.com/en-us/library/ie/ms536753(v=vs.85).aspx
And: https://developer.mozilla.org/en-US/docs/DOM/window.setTimeout#Callback_arguments

Davis W. Frank
Owner

We do need to support IE so please make sure any tests/implementation you add take that into account. Happy to comment on progressions on this. Thanks!

bertrand-pivotal

Does anyone know what the status on this is? We also need it on our project.

Davis W. Frank
Owner

We have fixed this on master. So 2.0 will behave properly. See https://github.com/pivotal/jasmine/blob/master/spec/core/ClockSpec.js

Davis W. Frank infews closed this March 05, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Oct 01, 2012
Add specs for passing arguments with setInterval and setTimeout 8bb84ff
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 23 additions and 1 deletion. Show diff stats Hide diff stats

  1. 24  spec/core/MockClockSpec.js
24  spec/core/MockClockSpec.js
... ...
@@ -1,7 +1,7 @@
1 1
 describe("MockClock", function () {
2 2
 
3 3
   beforeEach(function() {
4  
-    jasmine.Clock.useMock();    
  4
+    jasmine.Clock.useMock();
5 5
   });
6 6
 
7 7
   describe("setTimeout", function () {
@@ -14,6 +14,17 @@ describe("MockClock", function () {
14 14
       jasmine.Clock.tick(30001);
15 15
       expect(expected).toBe(true);
16 16
     });
  17
+
  18
+		it('should pass the specified arguments to the callback', function() {
  19
+			var aUnicorn = "a unicorn";
  20
+			var expectItToBeAUnicorn = function() {
  21
+				expect(arguments.length).toEqual(1);
  22
+				expect(arguments[0]).toBe(aUnicorn);
  23
+			};
  24
+
  25
+			setTimeout(expectItToBeAUnicorn, 0, aUnicorn);
  26
+			jasmine.Clock.tick(0);
  27
+		});
17 28
   });
18 29
 
19 30
   describe("setInterval", function () {
@@ -30,6 +41,17 @@ describe("MockClock", function () {
30 41
       jasmine.Clock.tick(1);
31 42
       expect(interval).toEqual(2);
32 43
     });
  44
+
  45
+		it('should pass the specified arguments to the callback', function() {
  46
+			var aUnicorn = "a unicorn";
  47
+			var expectItToBeAUnicorn = function() {
  48
+				expect(arguments.length).toEqual(1);
  49
+				expect(arguments[0]).toBe(aUnicorn);
  50
+			};
  51
+
  52
+			setInterval(expectItToBeAUnicorn, 0, aUnicorn);
  53
+			jasmine.Clock.tick(0);
  54
+		});
33 55
   });
34 56
 
35 57
   it("shouldn't complain if you call jasmine.Clock.useMock() more than once", function() {
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.