@@ -7,6 +7,10 @@ import ButtonsContainer from './ButtonsContainer';
7
7
8
8
const autoBind = require ( 'auto-bind' ) ;
9
9
10
+ // global variable for play function
11
+ let globalPlaying = false ;
12
+ let intervalId = null ;
13
+
10
14
class MainContainer extends Component {
11
15
constructor ( props ) {
12
16
super ( props ) ;
@@ -15,6 +19,7 @@ class MainContainer extends Component {
15
19
snapshotIndex : 0 ,
16
20
currentIndex : null ,
17
21
port : null ,
22
+ playing : false ,
18
23
} ;
19
24
20
25
autoBind ( this ) ;
@@ -46,30 +51,39 @@ class MainContainer extends Component {
46
51
const { snapshots, snapshotIndex } = this . state ;
47
52
if ( snapshots . length > 0 && snapshotIndex > 0 ) {
48
53
const newIndex = snapshotIndex - 1 ;
49
- this . handleJumpSnapshot ( newIndex ) ;
50
- this . setState ( { snapshotIndex : newIndex } ) ;
54
+ // second callback parameter of setState to invoke handleJumpSnapshot
55
+ this . setState ( { snapshotIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
51
56
}
52
57
}
53
58
54
59
moveForward ( ) {
55
60
const { snapshots, snapshotIndex } = this . state ;
56
61
if ( snapshotIndex < snapshots . length - 1 ) {
57
62
const newIndex = snapshotIndex + 1 ;
58
- this . handleJumpSnapshot ( newIndex ) ;
59
- this . setState ( { snapshotIndex : newIndex } ) ;
63
+ this . setState ( { snapshotIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
60
64
}
61
65
}
62
66
63
- playForward ( ) {
64
- const play = setInterval ( ( ) => {
65
- const { snapshots, snapshotIndex } = this . state ;
66
- if ( snapshotIndex < snapshots . length - 1 ) {
67
- const newIndex = snapshotIndex + 1 ;
68
- this . handleJumpSnapshot ( newIndex ) ;
69
- this . setState ( { snapshotIndex : newIndex } ) ;
70
- } else clearInterval ( play ) ;
71
- } , 1000 ) ;
72
- play ( ) ;
67
+ play ( ) {
68
+ globalPlaying = ! globalPlaying
69
+ this . setState ( { playing : globalPlaying } , ( ) => {
70
+ if ( this . state . playing ) {
71
+ intervalId = setInterval ( ( ) => {
72
+ const { snapshots, snapshotIndex } = this . state ;
73
+ if ( snapshotIndex < snapshots . length - 1 ) {
74
+ const newIndex = snapshotIndex + 1 ;
75
+ this . setState ( { snapshotIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
76
+ } else {
77
+ // clear interval when play reaches the end
78
+ globalVariable = false ;
79
+ clearInterval ( intervalId ) ;
80
+ this . setState ( { playing : false } )
81
+ }
82
+ } , 1000 ) ;
83
+ } else {
84
+ clearInterval ( intervalId ) ;
85
+ }
86
+ } )
73
87
}
74
88
75
89
emptySnapshot ( ) {
@@ -93,7 +107,7 @@ class MainContainer extends Component {
93
107
}
94
108
95
109
render ( ) {
96
- const { snapshots, snapshotIndex, port } = this . state ;
110
+ const { snapshots, snapshotIndex, port, playing } = this . state ;
97
111
return (
98
112
< div className = "main-container" >
99
113
< HeadContainer />
@@ -107,15 +121,14 @@ class MainContainer extends Component {
107
121
/>
108
122
< StateContainer snapshot = { snapshots [ snapshotIndex ] } />
109
123
< TravelContainer
110
- playing = { playing }
111
124
snapshotsLength = { snapshots . length }
125
+ snapshotIndex = { snapshotIndex }
112
126
handleChangeSnapshot = { this . handleChangeSnapshot }
113
127
handleJumpSnapshot = { this . handleJumpSnapshot }
114
- snapshotIndex = { snapshotIndex }
115
128
moveBackward = { this . moveBackward }
116
129
moveForward = { this . moveForward }
117
- playForward = { this . playForward }
118
- playing = { playing }
130
+ play = { this . play }
131
+ playing = { playing }
119
132
/>
120
133
< ButtonsContainer port = { port } />
121
134
</ div >
0 commit comments