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

javascript debounce

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 :: npm fs 
Javascript :: remove table line button html using javascript 
Javascript :: mongodb unwind 
Javascript :: date in react js 
Javascript :: get url parameters javascript 
Javascript :: react native grid view 
Javascript :: fuse.js 
Javascript :: material app routes 
Javascript :: render XML in node 
Javascript :: use location hook 
Javascript :: •“In React, everything is a component.” Explain 
Javascript :: how to change favicon dynamic in react js 
Javascript :: prime numbers using for loop in Js 
Javascript :: debug.xcconfig: unable to open file react native 
Javascript :: jquery not readonly 
Javascript :: use jq to update json file 
Javascript :: siwtch case javascript 
Javascript :: ejs include with variable 
Javascript :: settimeout javascript 
Javascript :: return promise in node js 
Javascript :: react declare multiple states 
Javascript :: js time function 
Javascript :: how to get date in footer javascript 
Javascript :: check / unchecked a checkbox with jQuery 
Javascript :: Xpath select Parent Node Based On Child Node 
Javascript :: apollo server change port 
Javascript :: ternary function javascript 
Javascript :: how to get width in javascript 
Javascript :: mobile number validation in javascript with country code 
Javascript :: nodejs read image as base64 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =