File tree 1 file changed +20
-5
lines changed
1 file changed +20
-5
lines changed Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ import quickSort from './algorithms/quickSort';
13
13
class App extends React . Component {
14
14
state = {
15
15
array : [ ] ,
16
- arraySteps : [ ] ,
16
+ timeouts : [ ] ,
17
17
algorithm : 'Bubble Sort' ,
18
18
barCount : 8 ,
19
19
delay : 96 ,
@@ -40,28 +40,43 @@ class App extends React.Component {
40
40
}
41
41
42
42
run ( array ) {
43
- array . map ( ( step , i ) =>
44
- setTimeout ( ( ) => {
43
+ this . clearTimeouts ( ) ;
44
+ let timeouts = [ ] ;
45
+
46
+ array . map ( ( step , i ) => {
47
+ let timeout = setTimeout ( ( ) => {
45
48
this . setState ( {
46
49
array : step ,
47
50
} )
48
- } , this . state . delay * i )
49
- ) ;
51
+ } , this . state . delay * i ) ;
52
+ timeouts . push ( timeout ) ;
53
+ } ) ;
54
+
55
+ this . setState ( {
56
+ timeouts : timeouts ,
57
+ } ) ;
50
58
}
51
59
52
60
changeAlgorithm = ( event ) => {
61
+ this . clearTimeouts ( ) ;
53
62
this . setState ( {
54
63
algorithm : event . target . value ,
55
64
} ) ;
56
65
} ;
57
66
58
67
changeDelay = ( event ) => {
68
+ this . clearTimeouts ( ) ;
59
69
this . setState ( {
60
70
delay : parseInt ( event . target . value ) ,
61
71
} ) ;
62
72
} ;
63
73
74
+ clearTimeouts = ( ) => {
75
+ this . state . timeouts . forEach ( timeout => clearTimeout ( timeout ) ) ;
76
+ }
77
+
64
78
generateBars = ( barCount ) => {
79
+ this . clearTimeouts ( ) ;
65
80
let barsTemp = [ ] ;
66
81
for ( let i = 0 ; i < barCount ; i ++ ) {
67
82
barsTemp . push ( Math . floor ( Math . random ( ) * 90 ) + 10 ) ;
You can’t perform that action at this time.
0 commit comments