Skip to content
This repository
  • 3 commits
  • 2 files changed
  • 7 comments
  • 2 contributors
21  test/functions.js
@@ -136,6 +136,16 @@ $(document).ready(function() {
136 136
     _.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 220);
137 137
   });
138 138
 
  139
+  asyncTest("functions: throttle recursively", 1, function() {
  140
+    var counter = 0;
  141
+    var incr = _.throttle(function() {
  142
+        counter++;
  143
+        if (counter < 5) incr();
  144
+    }, 100);
  145
+    incr();
  146
+    _.delay(function(){ equal(counter, 3, "incr was throttled"); start(); }, 220);
  147
+  });
  148
+
139 149
   asyncTest("functions: debounce", 1, function() {
140 150
     var counter = 0;
141 151
     var incr = function(){ counter++; };
@@ -163,6 +173,17 @@ $(document).ready(function() {
163 173
     _.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 220);
164 174
   });
165 175
 
  176
+  asyncTest("functions: debounce asap recursively", 2, function() {
  177
+    var counter = 0;
  178
+    var debouncedIncr = _.debounce(function(){
  179
+      counter++;
  180
+      if (counter < 5) debouncedIncr();
  181
+    }, 50, true);
  182
+    debouncedIncr();
  183
+    equal(counter, 1, 'incr was called immediately');
  184
+    _.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 70);
  185
+  });
  186
+
166 187
   test("functions: once", function() {
167 188
     var num = 0;
168 189
     var increment = _.once(function(){ num++; });
5  underscore.js
@@ -541,10 +541,10 @@
541 541
       if (throttling) {
542 542
         more = true;
543 543
       } else {
  544
+        throttling = true;
544 545
         result = func.apply(context, args);
545 546
       }
546 547
       whenDone();
547  
-      throttling = true;
548 548
       return result;
549 549
     };
550 550
   };
@@ -561,9 +561,10 @@
561 561
         timeout = null;
562 562
         if (!immediate) func.apply(context, args);
563 563
       };
564  
-      if (immediate && !timeout) func.apply(context, args);
  564
+      var callNow = immediate && !timeout;
565 565
       clearTimeout(timeout);
566 566
       timeout = setTimeout(later, wait);
  567
+      if (callNow) func.apply(context, args);
567 568
     };
568 569
   };
569 570
 

Showing you all comments on commits in this comparison.

John-David Dalton

This should be incr(); incr();

Jez Ng
int3 commented on 6e5d5b8

With a time limit of 220, incr() will indeed run thrice -- at 0, 100, and 200 ms. (The test passes as-is.)

John-David Dalton

It fails in IE6 and doesn't match the test above it which is basically the same thing except it calls:

  asyncTest("functions: throttle twice", 1, function() {
    var counter = 0;
    var incr = function(){ counter++; };
    var throttledIncr = _.throttle(incr, 100);
    throttledIncr(); throttledIncr();
    _.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 220);
  });
Jez Ng
int3 commented on 6e5d5b8

But throttledIncr in the above test doesn't call itself recursively, so it's expected that it only gets called twice in that case. Strange that it fails on IE6 -- maybe the time limit is too low. Does it still fail if you change 220 to 290?

John-David Dalton

But throttledIncr in the above test doesn't call itself recursively, so it's expected that it only gets called twice in that case

Ah right.

Does it still fail if you change 220 to 290?

Nope, I tried 500 and still it reports 2.
I'm out of ideas on why this is failing at the moment, I will sleep on it.

John-David Dalton

RE: Fails in IE8 too.

John-David Dalton

Opened issue #581.

Something went wrong with that request. Please try again.