Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Created December 7, 2015 19:51
Show Gist options
  • Save brianswisher/a041bfdc69ddb684de30 to your computer and use it in GitHub Desktop.
Save brianswisher/a041bfdc69ddb684de30 to your computer and use it in GitHub Desktop.
Debounce
((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