Guest User

Untitled

a guest
Oct 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. function debounce(fn, delay) {
  2. let timer;
  3.  
  4. function helper(...args) {
  5. clearTimeout(timer);
  6. timer = setTimeout(() => {
  7. fn.apply(this, args); // keep the original context
  8. }, delay);
  9. }
  10.  
  11. return helper;
  12. }
  13.  
  14. // ------------Test----------
  15. // var test = {
  16. // value: 'test',
  17. // changeInput: function(key) {
  18. // console.log('Input changed', key, this.value);
  19. // }
  20. // }
  21. // test.changeInput = debounce(test.changeInput, 1000);
  22.  
  23. // for(let i = 0; i <= 10; i++) {
  24. // test.changeInput(i);
  25. // }
  26.  
  27. // Output 'Input changed', 10, 'test'
  28. // Only triggered once.
Add Comment
Please, Sign In to add comment