Giter VIP home page Giter VIP logo

jquery-throttle-debounce's Introduction

jQuery jQuery throttle / debounce: Sometimes, less is more!

http://benalman.com/projects/jquery-throttle-debounce-plugin/

Version: 1.1, Last updated: 3/7/2010

jQuery throttle / debounce allows you to rate-limit your functions in multiple useful ways. Passing a delay and callback to $.throttle returns a new function that will execute no more than once every delay milliseconds. Passing a delay and callback to $.debounce returns a new function that will execute only once, coalescing multiple sequential calls into a single execution at either the very beginning or end.

Visit the project page for more information and usage examples!

Documentation

http://benalman.com/code/projects/jquery-throttle-debounce/docs/

Examples

These working examples, complete with fully commented code, illustrate a few ways in which this plugin can be used.

http://benalman.com/code/projects/jquery-throttle-debounce/examples/throttle/
http://benalman.com/code/projects/jquery-throttle-debounce/examples/debounce/

Support and Testing

Information about what version or versions of jQuery this plugin has been tested with, what browsers it has been tested in, and where the unit tests reside (so you can test it yourself).

jQuery Versions

none, 1.3.2, 1.4.2

Browsers Tested

Internet Explorer 6-8, Firefox 2-3.6, Safari 3-4, Chrome, Opera 9.6-10.1.

Unit Tests

http://benalman.com/code/projects/jquery-throttle-debounce/unit/

Release History

1.1 - (3/7/2010) Fixed a bug in jQuery.throttle where trailing callbacks executed later than they should. Reworked a fair amount of internal logic as well.
1.0 - (3/6/2010) Initial release as a stand-alone project. Migrated over from jquery-misc repo v0.4 to jquery-throttle repo v1.0, added the no_trailing throttle parameter and debounce functionality.

License

Copyright (c) 2010 "Cowboy" Ben Alman
Dual licensed under the MIT and GPL licenses.
http://benalman.com/about/license/

jquery-throttle-debounce's People

Contributors

cowboy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jquery-throttle-debounce's Issues

Separate delay for inactivity timeout

In my case, the responsiveness of the final call in a throttle is very important.
So I think I need a separate inactivity timeout.

For example, if some event happens during scrolls (say maybe every 5ms), it's nice to space out the actual callback calls to maybe 250ms, but once the scrolling is done, I would like the final callback call to come very quickly, maybe after 50ms of no calls to the throttled function. This is important to make things look right for the user.
I don't want to lower the current delay to 50ms because it slows down my page a lot.
Notice in my example that: 5 ms average interval < 50 ms < 250 ms

If you have no time for this, I could code it with a few tips from you to speed things up.

Does not work inside function()

Hello,

This works:

function test() {
    console.log('test');
}
$(window).on('scroll', $.throttle(250, test));

But this does not:

function test() {
    console.log('test');
}

 $(window).on('scroll', function() {
 	$.throttle(250, test);
 })

test() never gets called at all.

I need to call the throttled function if some specific circunstances happens, and I define these circunstances inside a function(). How can I use $.throttle then?

Example:

$('#check_subdomain').on('keyup', function() {
    if ($(this).val().length > 3) {
        $.throttle(1000, check_subdomain_availability_ajax);
    }
}

Adding support for observers

I'm not sure if this is possible but as MDN suggests that observers behave very similarly to event listeners, it might be worth the shot. The MutationObserver is such an observer. It's really useful and cool (it observes a DOM element as the name implies), but from a bit of testing I found that it encounters a lot of changes which in turn trigger many callbacks. It would be nice to be able to throttle and even debounce these!

debounce timeout documentation should be clarified

It should be clarified in the graphical and ASCII art documentation, that the callback will be invoked after the specified pause has elapsed since the last debounced call. I think in the present form it's quite misleading.

undefined jquery "guid" property

With the following HTML

  <html>
    <head>
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
      <script type="text/javascript" src="./jquery.ba-throttle-debounce.js"></script>

      <script type="text/javascript">
        $.debounce(function() {});
      </script>

    </head>
  </html>

I get the following traceback:

  Uncaught TypeError: Cannot read property 'guid' of undefined
    at "$.throttle.jq_throttle" in jquery.ba-throttle-debounce.js:190
    at "$.debounce" jquery.ba-throttle-debounce.js:248
    at (anonymous function) in foo.html:6

I am using Chromium 11 on Ubuntu.

Issue on FireFox

Works fine on Google Chrome, but the same is not working on Firefox's latest version.

image

would be nice to have timeout_id public

At the moment it's not possible to cancel the callback invocation (using clearTimeout).

Something like this would help a lot:

 // Return the wrapper function.
 wrapper.timeout_id = timeout_id;
 return wrapper;

[enhancement] Add missing bower.json.

Hey, maintainer(s) of cowboy/jquery-throttle-debounce!

We at VersionEye are working hard to keep up the quality of the bower's registry.

We just finished our initial analysis of the quality of the Bower.io registry:

7530 - registered packages, 224 of them doesnt exists anymore;

We analysed 7306 existing packages and 1070 of them don't have bower.json on the master branch ( that's where a Bower client pulls a data ).

Sadly, your library cowboy/jquery-throttle-debounce is one of them.

Can you spare 15 minutes to help us to make Bower better?

Just add a new file bower.json and change attributes.

{
  "name": "cowboy/jquery-throttle-debounce",
  "version": "1.0.0",
  "main": "path/to/main.css",
  "description": "please add it",
  "license": "Eclipse",
  "ignore": [
    ".jshintrc",
    "**/*.txt"
  ],
  "dependencies": {
    "<dependency_name>": "<semantic_version>",
    "<dependency_name>": "<Local_folder>",
    "<dependency_name>": "<package>"
  },
  "devDependencies": {
    "<test-framework-name>": "<version>"
  }
}

Read more about bower.json on the official spefication and nodejs semver library has great examples of proper versioning.

NB! Please validate your bower.json with jsonlint before commiting your updates.

Thank you!

Timo,
twitter: @versioneye
email: [email protected]
VersionEye - no more legacy software!

eventData?

I'm having a hell of a time passing eventData into throttle-debounce.

Is there something missing here? how can we pass values into this and still carry them to the callback?

passing parameters to callback?

Is it possible to pass arguments to the callback function?
e.g.:

$(window).scroll($.debounce(40, updateLeftOffset(el, offset)));

This example throws an error right now. Is there a specific reason why this is not desired?

Is this sweet little plugin still maintained?

Fails in jQuery 1.4.2 if callback is not defined

We had to add a check to verify that callback was defined as follows (line 189) - otherwise the wrapper.guid assignment failed catastrophically:

    // Set the guid of `wrapper` function to the same of original callback, so
    // it can be removed in jQuery 1.4+ .unbind or .die by using the original
    // callback as a reference.
    if ($.guid && callback) {
          wrapper.guid = callback.guid = callback.guid || $.guid++;
    }

d3.js mousemove

I am using d3.js library.

.on( "mousemove", $.throttle(1000, false, function() { console.log(d3.mouse(this) }) )

This returns only 1st result and some random good results afterwards. In general, it returns error:
[Error] TypeError: null is not an object (evaluating 't.sourceEvent')
b (d3.min.js, line 1)
mouse (d3.min.js, line 3)
(anonymous function) (index.html, line 74)
exec (jquery.ba-throttle-debounce.js, line 149)

If true is used for no_trailing it works but without the last event data (of course). I need false no_trailing.

Debouncing an existing an existing listener

Hey Ben,

I'm using FCBKcomplete, which is basically a Facebook-esque lookup via ajax for jQuery. However, the current implementation checks on every keystroke, which it does via it's own listener.

Here is how I'm using it on doc ready:

$("#tagselect").fcbkcomplete({
        json_url: "/gettags",
        cache: true,
        filter_case: false,
        filter_hide: true,
        firstselected: true,
        filter_selected: true,
        addontab: true,
        maxitems: 5,
        newel: true,
    });

Since it already has a listener for keypresses, how can I debounce this?

Much obliged,
Dennis

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.