Can someone explain the “debounce” function in Javascript

后端 未结 8 834
旧时难觅i
旧时难觅i 2020-11-22 02:57

I am interested in the \"debouncing\" function in javascript, written here : http://davidwalsh.name/javascript-debounce-function

Unfortunately the code is not explai

8条回答
  •  情深已故
    2020-11-22 03:22

    Simple Debounce method in javascript

    
    
    
    
      
      
      Debounce Method
    
    
      
    // JS File var debouncebtn = document.getElementById('debounce'); function debounce(func, delay){ var debounceTimer; return function () { var context = this, args = arguments; clearTimeout(debounceTimer); debounceTimer = setTimeout(function() { func.apply(context, args) }, delay); } } // Driver Code debouncebtn.addEventListener('click', debounce(function() { document.getElementById('message').innerHTML += '
    Button only triggeres is every 3 secounds how much every you fire an event'; console.log('Button only triggeres in every 3 secounds how much every you fire an event'); },3000))

    Runtime Example JSFiddle: https://jsfiddle.net/arbaazshaikh919/d7543wqe/10/

提交回复
热议问题