by
8
5
1,257
1
Top 1% !
Popular
Famous
Nice
Specified
Popularity: 17036th place
Created
Modified Aug 25, 2014
This snippet is Public
No tags for this snippet yet.
LanguageJavaScript
LicenseMIT_X11
SourceGitHub
SourceMyGists

A load balancing function....   debounce()

A load balancing function....   debounce(): 
debounce.js
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/143071/A-load-balancing-function-------debounce?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
/* The debounce method returns a function which wraps your callback, limiting its execution rate to the limit specified in the second argument. Now your frequent callbacks can't brick the user's browser! */ function debounce(func, wait, immediate) { var timeout; return function() { var context = this, args = arguments; var later = function() { timeout = null; if (!immediate) func.apply(context, args); }; var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) func.apply(context, args); }; } // execute once every 300 ms window.addEventListener('resize', debounce(function(event) { // (whatever you'd like to do!) }, 300));
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus