Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Initial commit.
- Loading branch information
0 parents
commit 3576e61
Showing
7 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
components | ||
build |
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
build: components index.js | ||
@component build --dev | ||
|
||
components: component.json | ||
@component install --dev | ||
|
||
clean: | ||
rm -fr build components template.js | ||
|
||
.PHONY: clean |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# throttle | ||
|
||
Restrict how frequently a function may be invoked. | ||
|
||
## Installation | ||
|
||
$ component install timoxley/throttle | ||
|
||
## Example | ||
|
||
```js | ||
var throttle = require('throttle') | ||
window.addEventListener('scroll', throttle(1000, function() { | ||
console.log('I should only happen once every second.') | ||
})) | ||
window.addEventListener('scroll', throttle(2000, function() { | ||
console.log('I should only happen once every two seconds.') | ||
})) | ||
``` | ||
|
||
## API | ||
|
||
### throttle(wait, fn) | ||
Returns a new function that wraps the supplied `fn`, blocking calls to `fn` until `wait` milliseconds have elapsed. | ||
|
||
## License | ||
|
||
MIT |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "throttle", | ||
"repo": "timoxley/throttle", | ||
"description": "Throttle how frequently a function may be invoked.", | ||
"version": "0.0.1", | ||
"keywords": ["underscore", "function"], | ||
"dependencies": {}, | ||
"development": { | ||
"timoxley/assert": "*" | ||
}, | ||
"license": "MIT", | ||
"scripts": [ | ||
"index.js" | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<html> | ||
<head> | ||
<title>Mocha</title> | ||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | ||
<style> | ||
body { | ||
min-height: 2000px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h4>Check console while scrolling page.</h4> | ||
<script src="/build/build.js"></script> | ||
<script> | ||
var throttle = require('throttle') | ||
var assert = require('timoxley-assert') | ||
window.addEventListener('scroll', throttle(1000, function(e) { | ||
console.log('I should only happen once every second.') | ||
// silly example to show arguments are preserved | ||
assert.ok(e instanceof Event) | ||
})) | ||
var anotherOnScroll = function(e) { | ||
console.log('I should only happen once every '+this.n+' seconds.') | ||
} | ||
// another silly example to show value of `this` is preserved. Note the bind. | ||
window.addEventListener('scroll', throttle(2000, anotherOnScroll.bind({n: 2}))) | ||
</script> | ||
</body> | ||
</html> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Returns a function, that, when invoked, will only be triggered at most once during a given window of time. | ||
* | ||
* @param {Number} wait milliseconds to wait before allowing another invokation. | ||
* @param {Number} fn target function to invoke. | ||
* @return {Function} Throttled function. | ||
* @api public | ||
*/ | ||
module.exports = (function() { | ||
return function throttle(wait, fn) { | ||
var blocked, timeout | ||
return function() { | ||
if (blocked) { | ||
return | ||
} | ||
blocked = true | ||
timeout = setTimeout(function() { | ||
blocked = false | ||
}, wait) | ||
return fn.apply(this, arguments) | ||
} | ||
} | ||
})() |