throttle.js

From disqus.com, 7 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to debounce.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/1a28c6c0 Embed
Download Paste or View Raw
  1. define('core/utils/function/throttle',[],function () {
  2.     'use strict';
  3.  
  4.     // Modified Underscore.js throttle implementation
  5.     // Trailing calls can be delayed more than the throttle delay
  6.     return function throttle(func, delay, additionalTrailingDelay) {
  7.         if (!additionalTrailingDelay)
  8.             additionalTrailingDelay = 0;
  9.  
  10.         var context, args, timeout, result;
  11.         var previous = 0;
  12.         var later = function () {
  13.             previous = new Date();
  14.             timeout = null;
  15.             result = func.apply(context, args);
  16.         };
  17.         return function () {
  18.             var now = new Date();
  19.             var remaining = delay - (now - previous);
  20.             context = this;  // eslint-disable-line consistent-this
  21.             args = arguments;
  22.             if (remaining <= 0) {
  23.                 clearTimeout(timeout);
  24.                 timeout = null;
  25.                 previous = now;
  26.                 result = func.apply(context, args);
  27.             } else if (!timeout) {
  28.                 timeout = setTimeout(later, remaining + additionalTrailingDelay);
  29.             }
  30.             return result;
  31.         };
  32.     };
  33. });
  34.  
  35. // https://c.disquscdn.com/next/next-core/core/utils/function/throttle.js
  36. // https://a.disquscdn.com/next/next-core/core/utils/function/throttle.js

Replies to throttle.js rss

Title Name Language When
social_login.js disqus.com javascript 7 Months ago.

Reply to "throttle.js"

Here you can reply to the paste above