Created
December 7, 2015 19:51
-
-
Save brianswisher/a041bfdc69ddb684de30 to your computer and use it in GitHub Desktop.
Debounce
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
((app)=>{ | |
var elapse = 0, | |
intv; | |
app.props.onEvent = app.debounce(app.run, app.props.DEBOUNCE_DEFER); | |
intv = setInterval(()=>{ | |
app.props.onEvent({ | |
app: app | |
}); | |
if (elapse >= app.props.MAX_TIME) { | |
console.log("done.") | |
clearInterval(intv); | |
return true; | |
} else { | |
console.log(elapse); | |
} | |
elapse += app.props.EVENT_INTV; | |
}, app.props.EVENT_INTV); | |
})({ | |
props:{ | |
DEBOUNCE_DEFER: 2000, | |
EVENT_INTV: 500, | |
MAX_TIME: 4000, | |
onEvent: null, | |
timeout: null, | |
}, | |
run: ()=>{ | |
console.log("my debounced function."); | |
}, | |
debounce: (func, defer)=>{ | |
return (event)=>{ | |
clearTimeout(event.app.props.timeout); | |
event.app.props.timeout = setTimeout(()=>{ | |
func(); | |
}, defer); | |
}; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment