tricks
Version:
ES6 modules
22 lines (17 loc) • 359 B
JavaScript
// Sleep
// Creates an instance of a function which
let i = 1;
const hash = {};
export default (callback, period = 0, guid = i++) => {
if (guid && hash[guid]) {
clearTimeout(hash[guid]);
delete hash[guid];
}
if (callback) {
// Set the period to change the state.
hash[guid] = setTimeout(() => {
callback();
}, period);
}
return guid;
};