debounce.js

From disqus.com, 7 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to each.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/76971a85 Embed
Download Paste or View Raw
  1. define('core/utils/function/debounce',[],function () {
  2.     'use strict';
  3.  
  4.     // NOTE: Ported from underscore 1.8.3
  5.     return function debounce(func, wait, immediate) {
  6.         var timeout, args, context, timestamp, result;
  7.  
  8.         var later = function () {
  9.             var last = new Date().getTime() - timestamp;
  10.  
  11.             if (last < wait && last >= 0) {
  12.                 timeout = setTimeout(later, wait - last);
  13.             } else {
  14.                 timeout = null;
  15.                 if (!immediate) {
  16.                     result = func.apply(context, args);
  17.                     if (!timeout) context = args = null;
  18.                 }
  19.             }
  20.         };
  21.  
  22.         return function () {
  23.             context = this;  // eslint-disable-line consistent-this
  24.             args = arguments;
  25.             timestamp = new Date().getTime();
  26.             var callNow = immediate && !timeout;
  27.             if (!timeout) timeout = setTimeout(later, wait);
  28.             if (callNow) {
  29.                 result = func.apply(context, args);
  30.                 context = args = null;
  31.             }
  32.  
  33.             return result;
  34.         };
  35.     };
  36. });
  37.  
  38. // https://c.disquscdn.com/next/next-core/core/utils/function/debounce.js
  39. // https://a.disquscdn.com/next/next-core/core/utils/function/debounce.js

Replies to debounce.js rss

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

Reply to "debounce.js"

Here you can reply to the paste above