Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

debounce js

function debounce(fn, delay) {
  let timer;
  return (() => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(), delay);
  })();
  
};

// usage
function someFn() {
  console.log('Hi')
};

debounce(someFn, 1000);
Comment

debounce js

const debounce = (fn, delay) => {
  let timer;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(fn, delay);
  };
};

// Example
let count = 0;
const increaseCount = (num) => {
  count += num;
  console.log(count);
};

window.addEventListener('scroll', debounce(increaseCount.bind(null, 5), 200));
Comment

debounce javascript

function debounce(func, timeout = 300){
  let timer;
  return function(...args) {
    if (timer) {
      clearTimeout(timer);
    }
    
    timer = setTimeout(() => {
      func.apply(this, args);
    }, timeout);
  };
}
Comment

debounce function in javascript

// Add this in HTML
<button id="myid">Click Me</button>

// This is JS Code for debounce function
const debounce = (fn,delay ) => {
  let timeoutID; // Initially undefined
  
  return function(...args){
    
    // cancel previously unexecuted timeouts
    if(timeoutID){
      clearTimeout(timeoutID);
    }
    
    timeoutID = setTimeout( () => {
      fn(...args);
    }, delay)
  }
}


document.getElementById('myid').addEventListener('click', debounce(e => {
  console.log('you clicked me');
}, 2000))
Comment

debounce javascript

let timeout;  

const debounce = (callback, wait) => {
    return (...args) => {
      clearTimeout(timeout);
      timeout = setTimeout(function () {
        callback.apply(this, args);
      }, wait);
    };
  };

const exampleFn = () => {
  console.log('Hello Word')
};

debounce(exampleFn, 1000);
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript define a global variable 
Javascript :: expressjs async await 
Javascript :: js is boolean 
Javascript :: javascript calculate aspect ratio 
Javascript :: js make id 
Javascript :: uploading file with fetch in js 
Javascript :: best and fastest encrypt and decrypt value in javascript 
Javascript :: ternary operator react 
Javascript :: javascript print path 
Javascript :: how to calculate the time complexity of a recursive function 
Javascript :: get element innerhtml jquery 
Javascript :: how to redirect in jquery onclick 
Javascript :: jquery is not defined rails 
Javascript :: how to change the first Letter to uppercase js 
Javascript :: background image in react from variable 
Javascript :: instantiate template playcanvas 
Javascript :: javascript option yes/no popup 
Javascript :: how to check if all inputs are not empty with javascript 
Javascript :: useeffect async await 
Javascript :: boucle for in js 
Javascript :: find and filter 
Javascript :: how would you check if a number is an integer in javascript 
Javascript :: or operator in javascript 
Javascript :: how to icon font-awesome react cart 
Javascript :: get font size jquery 
Javascript :: string to jspn js 
Javascript :: react-file-base64 
Javascript :: how to add youtube videos to react app 
Javascript :: check for duplicates in array javascript 
Javascript :: jest to include text 
ADD CONTENT
Topic
Content
Source link
Name
6+7 =