Uploaded image for project: 'Titanium SDK/CLI'
  1. Titanium SDK/CLI
  2. TIMOB-14213

Android: Exception thrown when using Underscore debounce

    Details

    • Sprint:
      2014 Sprint 11 SDK, 2014 Sprint 12 SDK

      Description

      Problem

      When trying to use the underscore function debounce within titanium on android to stop double clicks an exception is thrown:

      Exception occurred at ti:/bootstrap.js:131: Uncaught Error: Invalid value, expected type Number.

      Test case

      index.xml

      <Alloy>
          <Window>
              <Button onClick="testMe">Button</Button>
          </Window>
      </Alloy>
      

      index.js

      $.index.open();
       
      var testMe = _.debounce(function() {
          console.info('test me!');
      }, 300, true);
      

      Logs

      console

      E/TitaniumModule( 5553): Invalid value, expected type Number.
      E/TiExceptionHandler( 5553): (main) [466764,495427] ----- Titanium Javascript Runtime Error -----
      E/TiExceptionHandler( 5553): (main) [0,495427] - In ti:/bootstrap.js:131,32
      E/TiExceptionHandler( 5553): (main) [1,495428] - Message: Uncaught Error: Invalid value, expected type Number.
      E/TiExceptionHandler( 5553): (main) [1,495429] - Source: return Titanium.clearTimeout.apply(Titanium, arguments);
      E/V8Exception( 5553): Exception occurred at ti:/bootstrap.js:131: Uncaught Error: Invalid value, expected type Number.
      

      Discussions

      There was another ticket opened for this error by another user but it was closed due to inactivity from the original poster. This issue can be found here.

      The issue can be over come by altering the original debounce function by assigning the variable timeout the initial value "null". This highlights a possible issue with the Titanium clearTimeout implementation?

      original

      _.debounce = function(func, wait, immediate) {
          var timeout, result;
          return function() {
              var context = this, args = arguments;
              var later = function() {
                  timeout = null;
                  if (!immediate) result = func.apply(context, args);
              };
              
              var callNow = immediate && !timeout;
              clearTimeout(timeout);
              timeout = setTimeout(later, wait);
              if (callNow) result = func.apply(context, args);
              return result;
          };
      };
      

      altered

      _.debounce = function(func, wait, immediate) {
          var timeout = null;
          var result = null;
          return function() {
              var context = this, args = arguments;
              var later = function() {
                  timeout = null;
                  if (!immediate) result = func.apply(context, args);
              };
              
              var callNow = immediate && !timeout;
              clearTimeout(timeout);
              timeout = setTimeout(later, wait);
              if (callNow) result = func.apply(context, args);
              return result;
          };
      };
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sunila Sunila
                Reporter:
                rowan rowan smith
                Reviewer:
                Hieu Pham
              • Watchers:
                0 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Backbone Issue Sync

                  • Titanium SDK/CLI <> Titanium Mobile
                    Synced with:
                    TIMOB-16731
                    Sync status:
                    ERROR
                    Last received:
                    Last sent:

                    Git Integration