Last active
November 22, 2016 11:35
-
-
Save berkin/ad475637598e1f9d24cbbd8d8076e9a7 to your computer and use it in GitHub Desktop.
This file contains 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
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