| OLD | NEW |
| (Empty) |
| 1 | |
| 2 | |
| 3 Polymer.Debounce = (function() { | |
| 4 | |
| 5 // usage | |
| 6 | |
| 7 // invoke cb.call(this) in 100ms, unless the job is re-registered, | |
| 8 // which resets the timer | |
| 9 // | |
| 10 // this.job = this.debounce(this.job, cb, 100) | |
| 11 // | |
| 12 // returns a handle which can be used to re-register a job | |
| 13 | |
| 14 var Async = Polymer.Async; | |
| 15 | |
| 16 var Debouncer = function(context) { | |
| 17 this.context = context; | |
| 18 this.boundComplete = this.complete.bind(this); | |
| 19 }; | |
| 20 | |
| 21 Debouncer.prototype = { | |
| 22 go: function(callback, wait) { | |
| 23 var h; | |
| 24 this.finish = function() { | |
| 25 Async.cancel(h); | |
| 26 }; | |
| 27 h = Async.run(this.boundComplete, wait); | |
| 28 this.callback = callback; | |
| 29 }, | |
| 30 stop: function() { | |
| 31 if (this.finish) { | |
| 32 this.finish(); | |
| 33 this.finish = null; | |
| 34 } | |
| 35 }, | |
| 36 complete: function() { | |
| 37 if (this.finish) { | |
| 38 this.stop(); | |
| 39 this.callback.call(this.context); | |
| 40 } | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 function debounce(debouncer, callback, wait) { | |
| 45 if (debouncer) { | |
| 46 debouncer.stop(); | |
| 47 } else { | |
| 48 debouncer = new Debouncer(this); | |
| 49 } | |
| 50 debouncer.go(callback, wait); | |
| 51 return debouncer; | |
| 52 } | |
| 53 | |
| 54 // exports | |
| 55 | |
| 56 return debounce; | |
| 57 | |
| 58 })(); | |
| 59 | |
| OLD | NEW |