45

I have a keyup event bound to a function that takes about a quarter of a second to complete.

$("#search").keyup(function() {
  //code that takes a little bit to complete
});

When a user types an entire word, or otherwise presses keys rapidly, the function will be called several times in succession and it will take a while for them all to complete.

Is there a way to throttle the event calls so that if there are several in rapid succession, it only triggers the one that was most recently called?

3

7 Answers 7

68

Take a look at jQuery Debounce.

$('#search').keyup($.debounce(function() {
    // Will only execute 300ms after the last keypress.
}, 300));
10
  • 1
    this does not work when i use multiple selector like .sel1, .sel2 sel3 Sep 25, 2012 at 15:55
  • 2
    @HungryCoder: It does work. Of course, every matching element will share the same timer, so the callback won't fire until after you completely stop sending events.
    – josh3736
    Sep 25, 2012 at 16:36
  • 3
    Debounce isn't throttling..it's something else
    – vsync
    Mar 17, 2014 at 20:18
  • @vsync: Nitpicking terminology. OP said "throttle" but really meant "debounce"
    – josh3736
    Mar 17, 2014 at 20:22
  • 8
    This does not answer the question. The OP may think it has been answered, but this is not even halfway toward what is described, and in no way approximates expected behaviour for an autocomplete result. Debounce is the wrong function. OP is actually asking for throttle. See: css-tricks.com/the-difference-between-throttling-and-debouncing
    – Aeschylus
    Jul 13, 2015 at 0:53
12

Here is a potential solution that doesn't need a plugin. Use a boolean to decide whether to do the keyup callback, or skip over it.

var doingKeyup = false;

$('input').keyup(function(){
    if(!doingKeyup){
        doingKeyup=true;
        // slow process happens here
        doingKeyup=false;
    }
});
7
  • 2
    This is a throttle, not a debounce. In the case of a search field, you want to run the search after the user has finished typing (a debounce), not after they've typed one character (a throttle). The Debounce plugin is really just a <1 kB convenience method that wraps calls to setTimeout/clearTimeout.
    – josh3736
    Sep 10, 2011 at 17:19
  • 1
    I'm not making assumptions about how his search field is intended to work. Sep 10, 2011 at 17:35
  • 1
    @josh3736 To be fair, the question title did ask for a "throttle". I hadn't heard of "debounce", before, though. Sep 11, 2011 at 1:53
  • 1
    There is one other problem with this approach -- it won't fire on the final text string if last character or a series of characters were entered while the previous handler was running. Aug 19, 2012 at 21:34
  • 2
    Only if // slow process happens in another event loop, this would be right way otherwise since JS is single-threaded this is not useful. Feb 17, 2015 at 10:45
6

You could also use the excellent Underscore/_ library.

Comments in Josh's answer, currently the most popular, debate whether you should really throttle the calls, or if a debouncer is what you want. The difference is a bit subtle, but Underscore has both: _.debounce(function, wait, [immediate]) and _.throttle(function, wait, [options]).

If you're not already using Underscore, check it out. It can make your JavaScript much cleaner, and is lightweight enough to give most library haters pause.

2

Here's a clean way of doing it with JQuery.

    /* delayed onchange while typing jquery for text boxes widget
    usage:
        $("#SearchCriteria").delayedChange(function () {
            DoMyAjaxSearch();
        });

    */
    (function ($) {
        $.fn.delayedChange = function (options) {
            var timer;
            var o;

            if (jQuery.isFunction(options)) {
                o = { onChange: options };
            }
            else
                o = options;

            o = $.extend({}, $.fn.delayedChange.defaultOptions, o);

            return this.each(function () {
                var element = $(this);
                element.keyup(function () {
                    clearTimeout(timer);
                    timer = setTimeout(function () {
                        var newVal = element.val();
                        newVal = $.trim(newVal);
                        if (element.delayedChange.oldVal != newVal) {
                            element.delayedChange.oldVal = newVal;
                            o.onChange.call(this);
                        }

                    }, o.delay);
                });
            });


        };

        $.fn.delayedChange.defaultOptions = {
            delay: 1000,
            onChange: function () { }
        }

        $.fn.delayedChange.oldVal = "";


    })(jQuery);
0
1

Two small generic implementations of throttling approaches. (I prefer to do it through these simple functions rather than adding another jquery plugin)

  1. Waits some time after last call

    This one is useful when we don't want to call for example search function when user keeps typing the query

function throttle(time, func) {
  if (!time || typeof time !== "number" || time < 0) {
      return func;
  }

  var throttleTimer = 0;

  return function() {
    var args = arguments;
    clearTimeout(throttleTimer);
    throttleTimer = setTimeout(function() {
      func.apply(null, args);
    }, time);
  }
}
  1. Calls given function not more often than given amount of time

    The following one is useful for flushing logs

function throttleInterval(time, func) {
  if (!time || typeof time !== "number" || time < 0) {
      return func;
  }

  var throttleTimer = null;
  var lastState = null;
  var eventCounter = 0;
  var args = [];

  return function() {
    args = arguments;
    eventCounter++;
    if (!throttleTimer) {
      throttleTimer = setInterval(function() {
        if (eventCounter == lastState) {
          clearInterval(throttleTimer);
          throttleTimer = null;
          return;
        }

        lastState = eventCounter;
        func.apply(null, args);
      }, time);
    }
  }
}

Usage is very simple:

The following one is waiting 2s after the last keystroke in the inputBox and then calls function which should be throttled.

$("#inputBox").on("input", throttle(2000, function(evt) {
  myFunctionToThrottle(evt);
}));

Here is an example where you can test both: click (CodePen)

1
  • Both examples are debounce methods, not throttle ;)
    – Chaoste
    Nov 2, 2020 at 9:55
-1

I came across this question reviewing changes to . They've added their own method for debounce and throttling. It looks like it might be the same as the jquery-debounce @josh3736 mentioned in his answer.

From their website:

// Debounced button click handler
$('.button').on('click', Foundation.utils.debounce(function(e){
  // Handle Click
}, 300, true));

// Throttled resize function
$(document).on('resize', Foundation.utils.throttle(function(e){
  // Do responsive stuff
}, 300));
-4

Something like this seems simplest (no external libraries) for a quick solution (note coffeescript):

running = false
$(document).on 'keyup', '.some-class', (e) ->
  return if running
  running = true
  $.ajax
    type: 'POST',
    url: $(this).data('url'),
    data: $(this).parents('form').serialize(),
    dataType: 'script',
    success: (data) ->
      running = false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.