forked from Polymer/polymer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add debounce (formerly known as job), tests, docs.
- Loading branch information
1 parent
1bc61f3
commit 5188704
Showing
4 changed files
with
243 additions
and
14 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
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,73 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<script> | ||
|
||
modulate('Debounce', function() { | ||
|
||
// usage | ||
|
||
// invoke cb.call(this) in 100ms, unless the job is re-registered, | ||
// which resets the timer | ||
// | ||
// this.myJob = this.job(this.myJob, cb, 100) | ||
// | ||
// returns a job handle which can be used to re-register a job | ||
|
||
var Debouncer = function(inContext) { | ||
this.context = inContext; | ||
this.boundComplete = this.complete.bind(this) | ||
}; | ||
Debouncer.prototype = { | ||
go: function(callback, wait) { | ||
this.callback = callback; | ||
var h; | ||
if (!wait) { | ||
h = requestAnimationFrame(this.boundComplete); | ||
this.handle = function() { | ||
cancelAnimationFrame(h); | ||
}; | ||
} else { | ||
h = setTimeout(this.boundComplete, wait); | ||
this.handle = function() { | ||
clearTimeout(h); | ||
}; | ||
} | ||
}, | ||
stop: function() { | ||
if (this.handle) { | ||
this.handle(); | ||
this.handle = null; | ||
} | ||
}, | ||
complete: function() { | ||
if (this.handle) { | ||
this.stop(); | ||
this.callback.call(this.context); | ||
} | ||
} | ||
}; | ||
|
||
function debounce(debouncer, callback, wait) { | ||
if (debouncer) { | ||
debouncer.stop(); | ||
} else { | ||
debouncer = new Debouncer(this); | ||
} | ||
debouncer.go(callback, wait); | ||
return debouncer; | ||
} | ||
|
||
// exports | ||
|
||
return debounce; | ||
|
||
}); | ||
|
||
</script> |
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
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