Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Initial commit.
  • Loading branch information
timoxley committed Nov 4, 2012
0 parents commit 3576e61
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
components
build
Empty file added History.md
Empty file.
11 changes: 11 additions & 0 deletions Makefile
@@ -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
28 changes: 28 additions & 0 deletions Readme.md
@@ -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
15 changes: 15 additions & 0 deletions component.json
@@ -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"
]
}
29 changes: 29 additions & 0 deletions example/scroll.html
@@ -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>
23 changes: 23 additions & 0 deletions index.js
@@ -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)
}
}
})()

0 comments on commit 3576e61

Please sign in to comment.