util.ts 446 B

123456789101112131415161718192021222324252627
  1. import * as d3 from 'd3'
  2. export class Player {
  3. private oneStep: () => void
  4. private timerIndex = 0
  5. constructor(oneStep: () => void) {
  6. this.oneStep = oneStep
  7. }
  8. pause() {
  9. this.timerIndex++
  10. }
  11. play() {
  12. this.start(this.timerIndex)
  13. }
  14. private start(localTimeIndex: number) {
  15. const timer = d3.timer(() => {
  16. if (localTimeIndex < this.timerIndex) {
  17. timer.stop()
  18. }
  19. this.oneStep()
  20. })
  21. }
  22. }