Skip to content

Instantly share code, notes, and snippets.

@yckart
Forked from 140bytes/LICENSE.txt
Last active October 10, 2015 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yckart/3729753 to your computer and use it in GitHub Desktop.
Save yckart/3729753 to your computer and use it in GitHub Desktop.
throttle: Delays a function to a specified number of milliseconds.
function(
a, // the function
b, // a delay (optional)
c // timer-placeholder
) {
return function(){
if (c) return; // timer is truthy, stop here
c = setTimeout(function(){ c = 0 }, b || 100);
a.apply(this, arguments) // call the function
}
}
function(a,b,c){return function(){if(c)return;c=setTimeout(function(){c=0},b||100);a.apply(this,arguments)}}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2012 Yannick Albert <http://yckart.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "throttle",
"description": "Delays a function to a specified number of milliseconds.",
"keywords": [
"delay",
"throttle",
"wait",
"function",
"functional"
]
}
<!DOCTYPE html>
<title>throttle: Delays an event to a specified number of milliseconds.</title>
<b id="ret">Move your mouse and check the output.</b>
<script>
var throttle = function(a,b,c){return function(){if(c)return;c=setTimeout(function(){c=0},b||100);a.apply(this,arguments)}};
document.onmousemove = throttle(function() {
document.getElementById('ret').innerHTML = new Date().getTime();
}, 400);
</script>
@atk
Copy link

atk commented Sep 17, 2012

You can initialize c with false with 2 fewer bytes; instead of c=null; you can use c=!1;

@yckart
Copy link
Author

yckart commented Sep 22, 2012

Nice, 've updated!

@atk
Copy link

atk commented Sep 24, 2012

You're welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment