Skip to content

Instantly share code, notes, and snippets.

@MrMjauh
Last active June 6, 2019 07:29
Show Gist options
  • Save MrMjauh/4b39024f1c272a5c286ef4678f7642f8 to your computer and use it in GitHub Desktop.
Save MrMjauh/4b39024f1c272a5c286ef4678f7642f8 to your computer and use it in GitHub Desktop.
class VariableIntervaller {
private id: number = 0;
private dic: any = {};
public start(work: () => void, getTimeout: () => number): number {
const newID: number = this.id++;
this.dic[newID] = {
run: true,
intervaller: undefined
};
this.dic[newID].intervaller = setTimeout(function inner() {
work();
if (this.dic[newID].run) {
this.dic[newID].intervaller = setTimeout(inner, getTimeout());
} else {
delete this.dic[newID];
}
});
return newID;
}
public flagForRemoval(id: number): void {
if (id in this.dic) {
this.dic[id].run = false;
}
}
}
export const DynamicIntervalHandler = new VariableIntervaller();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment