Guest User

Untitled

a guest
Oct 5th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* tagDiv Newspaper live Search debounce hack */
  2.  
  3. jQuery(function() {
  4.   jQuery('.tdb-head-search-form-input').on('keydown', function(e) {
  5.     if (typeof debounce_do_ajax_call === "undefined") {
  6.       console.log('tdbSearch.do_ajax_call override');
  7.      
  8.       tdbSearch.original_td_do_ajax_call = tdbSearch.do_ajax_call;
  9.  
  10.       // as hacked out of context, needed to hook context or debounce would trigger on each call
  11.       debounce_do_ajax_call = debounce(tdbSearch.original_td_do_ajax_call, 1500)
  12.      
  13.       tdbSearch.do_ajax_call = debounce_do_ajax_call
  14.     }
  15.   });
  16. });
  17.  
  18. // https://davidwalsh.name/javascript-debounce-function
  19. function debounce(func, wait, immediate) {
  20.   var timeout;
  21.   return function() {
  22.     var context = this, args = arguments;
  23.     var later = function() {
  24.       timeout = null;
  25.       if (!immediate) func.apply(context, args);
  26.     };
  27.     var callNow = immediate && !timeout;
  28.     clearTimeout(timeout);
  29.     timeout = setTimeout(later, wait);
  30.     if (callNow) func.apply(context, args);
  31.   };
  32. };
Advertisement
Add Comment
Please, Sign In to add comment