Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Function Throttling

I want to use a JS Throttle. But i'm struggeling to get it work correctly.

I tried the code from this article: https://codeburst.io/throttling-and-debouncing-in-javascript-b01cad5c8edf

But the Throttle does not work as intended, since everytime i click on the button, one "|" is added to the div. No clicks were discarded.

where is the misstake?

function foo() {
	$("#respond").append("|");
}

const throttle = (func, limit) => {
  let inThrottle
  return function() {
    const args = arguments
    const context = this
    if (!inThrottle) {
      func.apply(context, args)
      inThrottle = true
      setTimeout(() => inThrottle = false, limit)
    }
  }
}

var onClick = function() {
    throttle(foo(), 50000);
};

$('#button').click(onClick);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="button" value="Click Me" />
<div id="respond"></div>
like image 392
CL90 Avatar asked Oct 18 '18 06:10

CL90


People also ask

What is difference between Debouncing and throttling?

It's simple. They do the exact same thing (rate limiting) except while throttle is being called it'll fire your function periodically, while debounce only fires once at the end. Example: If you're scrolling, throttle will slowly call your function while you scroll (every X milliseconds).

What is throttling and Debouncing in JavaScript?

Like debounce, throttle is also used to limit the number of times a function is called, but, unlike debounce, throttle will call the function passed to it every time the delay ends as long as the trigger for the function is still happening.

What is debounce in JavaScript?

The debounce() function forces a function to wait a certain amount of time before running again. The function is built to limit the number of times a function is called.

What is throttle in coding?

API throttling is the process of limiting the number of API requests a user can make in a certain period. An application programming interface (API) functions as a gateway between a user and a software application.


2 Answers

In order for throttle(func, limit) to work, there can only be one instance of its product.

The problem is that the onClick function in your example creates a new instance each time it is called.

This makes the underlying inThrottle variable meaningless, as a new copy is created for each click.

The solution is to call one single instance the product of throttle(foo, 50000) directly.

Also, foo itself should be passed (not its product).

See below for a practical example, as well as closures and scope for more info.

// Foo.
const foo = (...args) => {
  $("#respond").append("|");
}

// Throttle.
const throttle = (func, limit) => {
  let inThrottle
  return (...args) => {
    if (!inThrottle) {
      func(...args)
      inThrottle = setTimeout(() => inThrottle = false, limit)
    }
  }
}

// On Click.
const onClick = throttle(foo, 1000)

// Button - Click.
$('#button').click(onClick);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" id="button" value="Click Me" />
<div id="respond"></div>
like image 57
Arman Charan Avatar answered Sep 20 '22 23:09

Arman Charan


Your onClick is creating a new throttled function on every invoke. You have to ensure that is only throttled once

var onClick = function() {
    throttle(foo(), 50000);
};
// TO

var onClick = throttle(foo, 50000);
like image 29
Austio Avatar answered Sep 22 '22 23:09

Austio