Last active
August 29, 2015 14:05
-
-
Save 7cc/ed05b879f0b71b337887 to your computer and use it in GitHub Desktop.
resize, scrolll event for delay without lib
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
| var delayResize = { | |
| delays: [300, 600, 900], | |
| timers: [], | |
| clearAlltimers: function() { | |
| var l = this.timers.length | |
| for(var i=0; i<l; i++){ | |
| clearTimeout(this.timers[i]) | |
| } | |
| }, | |
| dispachEv: function(e){ | |
| window.dispatchEvent(new Event(e)) | |
| }, | |
| handleEvent: function() { | |
| this.clearAlltimers() | |
| this.delays.forEach(function(e,i) { | |
| // this.timers[i] = setTimeout(this.log, e, i) | |
| this.timers[i] = setTimeout(this.dispachEv, e, `delay${e}`) | |
| }, delayResize) | |
| } | |
| } | |
| window.addEventListener('resize', delayResize) | |
| // example | |
| var logEvType = (ev)=> console.log(ev.type) | |
| window.addEventListener('delay300', logEvType) | |
| window.addEventListener('delay600', logEvType) | |
| window.addEventListener('delay900', logEvType) |
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
| window.addEventListener("resize", { | |
| timer400: "", | |
| handleEvent: function(){ | |
| clearTimeout(this.timer400) | |
| this.timer400 = setTimeout(function(){ | |
| window.dispatchEvent(new Event("delayResize:400")) | |
| }, 400) | |
| } | |
| }, false) | |
| window.addEventListener("delayResize:400", function(ev){ | |
| console.log(ev.type) | |
| }, false) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment