Skip to content

Instantly share code, notes, and snippets.

@berkin
Last active November 22, 2016 11:35
Show Gist options
  • Save berkin/ad475637598e1f9d24cbbd8d8076e9a7 to your computer and use it in GitHub Desktop.
Save berkin/ad475637598e1f9d24cbbd8d8076e9a7 to your computer and use it in GitHub Desktop.
function Intent() {
const self = this;
let timer;
this.exec = function (callback, duration, ...args) {
self.clear();
timer = setTimeout(callback.bind(this, args), duration || 500);
};
this.clear = function() {
clearTimeout(timer);
};
}
export default Intent;
// usage
import Intent from 'intent';
const instance = new Intent();
document.querySelector('.element').addEventListener('input', () => {
instance.exec(callback.bind(e.target));
}, false);
const instances = [];
function Intent() {
const self = this;
let timer;
this.exec = function (callback, duration, ...args) {
self.clear();
timer = setTimeout(callback.bind(this, args), duration || 1000);
};
this.clear = function () {
clearTimeout(timer);
};
}
function exec(id) {
if (!instances[id]) {
instances[id] = new Intent();
}
return instances[id];
}
function clear(id) {
if (instances[id]) {
instances[id].clear();
delete instances[id];
}
}
export default {exec, clear};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment