-
-
Save yckart/3729753 to your computer and use it in GitHub Desktop.
throttle: Delays a function to a specified number of milliseconds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c){return function(){if(c)return;c=setTimeout(function(){c=0},b||100);a.apply(this,arguments)}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "throttle", | |
"description": "Delays a function to a specified number of milliseconds.", | |
"keywords": [ | |
"delay", | |
"throttle", | |
"wait", | |
"function", | |
"functional" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
Nice, 've updated!
You're welcome :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can initialize c with false with 2 fewer bytes; instead of c=null; you can use c=!1;