Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8812137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:32:12+00:00 2026-06-14T03:32:12+00:00

The underscore library provides a debounce function that prevents multiple calls to a function

  • 0

The underscore library provides a debounce function that prevents multiple calls to a function within a set period of time. Their version makes use of setTimeout.

How could we do this in pure AngularJS code?

Moreover, can we make use of $q style promises to retrieve the return value from the called function after the debounce period?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:32:13+00:00Added an answer on June 14, 2026 at 3:32 am

    Here is a working example of such a service: http://plnkr.co/edit/fJwRER?p=preview.
    It creates a $q deferred object that will be resolved when the debounced function is finally called.

    Each time the debounce function is called the promise to the next call of the inner function is returned.

    // Create an AngularJS service called debounce
    app.factory('debounce', ['$timeout','$q', function($timeout, $q) {
      // The service is actually this function, which we call with the func
      // that should be debounced and how long to wait in between calls
      return function debounce(func, wait, immediate) {
        var timeout;
        // Create a deferred object that will be resolved when we need to
        // actually call the func
        var deferred = $q.defer();
        return function() {
          var context = this, args = arguments;
          var later = function() {
            timeout = null;
            if(!immediate) {
              deferred.resolve(func.apply(context, args));
              deferred = $q.defer();
            }
          };
          var callNow = immediate && !timeout;
          if ( timeout ) {
            $timeout.cancel(timeout);
          }
          timeout = $timeout(later, wait);
          if (callNow) {
            deferred.resolve(func.apply(context,args));
            deferred = $q.defer();
          }
          return deferred.promise;
        };
      };
    }]);
    

    You get the return value from the debounced function by using the then method on the promise.

    $scope.addMsg = function(msg) {
        console.log('addMsg called with', msg);
        return msg;
    };
    
    $scope.addMsgDebounced = debounce($scope.addMsg, 2000, false);
    
    $scope.logReturn = function(msg) {
        console.log('logReturn called with', msg);
        var promise = $scope.addMsgDebounced(msg);
        promise.then(function(msg) {
            console.log('Promise resolved with', msg);
        });
    };
    

    If you call logReturn multiple times in quick succession you will see the logReturn call logged over and over but only one addMsg call logged.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I started reading about underscore.js today, it is a library for javascript that adds
Does anyone know of a Javascript library (e.g. underscore, jQuery, MooTools, etc.) that offers
I have been looking underscore.js library functions and I noticed a function which returns
When going through the underscore.js library, i came across for (var i = 0,
Does [_\s^] mean underscore and whitespace but not (quote) in Reg I understand that
I've built a system that uses underscore-js to render templates, and it works fine
How to set up variables inside an underscore.js template for an app built with
I was looking at the source code for the Underscore.js library, specifically for the
I would like to ask if there's any Java package or library that have
Using the javascript library underscore.js (v.1.3.1), I've reproduced the following on the mac in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.