Skip to content

Commit 0afd9b9

Browse files
committed
handle interruptions
1 parent 5612294 commit 0afd9b9

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/App.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import quickSort from './algorithms/quickSort';
1313
class App extends React.Component {
1414
state = {
1515
array: [],
16-
arraySteps: [],
16+
timeouts: [],
1717
algorithm: 'Bubble Sort',
1818
barCount: 8,
1919
delay: 96,
@@ -40,28 +40,43 @@ class App extends React.Component {
4040
}
4141

4242
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(() => {
4548
this.setState({
4649
array: step,
4750
})
48-
}, this.state.delay * i)
49-
);
51+
}, this.state.delay * i);
52+
timeouts.push(timeout);
53+
});
54+
55+
this.setState({
56+
timeouts: timeouts,
57+
});
5058
}
5159

5260
changeAlgorithm = (event) => {
61+
this.clearTimeouts();
5362
this.setState({
5463
algorithm: event.target.value,
5564
});
5665
};
5766

5867
changeDelay = (event) => {
68+
this.clearTimeouts();
5969
this.setState({
6070
delay: parseInt(event.target.value),
6171
});
6272
};
6373

74+
clearTimeouts = () => {
75+
this.state.timeouts.forEach(timeout => clearTimeout(timeout));
76+
}
77+
6478
generateBars = (barCount) => {
79+
this.clearTimeouts();
6580
let barsTemp = [];
6681
for (let i = 0; i < barCount; i++) {
6782
barsTemp.push(Math.floor(Math.random() * 90) + 10);

0 commit comments

Comments
 (0)