Last active
June 6, 2019 07:29
-
-
Save MrMjauh/4b39024f1c272a5c286ef4678f7642f8 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
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