Skip to content

Commit 9a2237c

Browse files
authored
Merge pull request #701 from jeongsd/master
cube setInterval -> requestAnimationFrame
2 parents 3512d57 + 3072fa4 commit 9a2237c

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

components/cube/cube.jsx

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,22 @@ export default class Cube extends React.Component {
7878
let degrees = 0;
7979
let axis = 'y';
8080

81-
this._interval = setInterval(() => {
82-
let obj = {};
83-
obj[axis] = degrees += 90;
84-
85-
this.setState({ ...obj, iteration: (this.state.iteration + 1) % 4 });
86-
}, repeatDelay);
81+
let lastTime = performance.now();
82+
let animation = () => {
83+
let nowTime = performance.now();
84+
let deltaTime = nowTime - lastTime;
85+
86+
if (repeatDelay <= deltaTime) {
87+
let obj = {};
88+
obj[axis] = degrees += 90;
89+
90+
this.setState({ ...obj, iteration: (this.state.iteration + 1) % 4 });
91+
lastTime = performance.now();
92+
}
93+
94+
this._requestAnimation = requestAnimationFrame(animation);
95+
};
96+
animation();
8797
}
8898
}
8999

@@ -95,7 +105,7 @@ export default class Cube extends React.Component {
95105
this.container.removeEventListener('mouseleave', this.listeners.reset);
96106

97107
} else if (continuous) {
98-
clearInterval(this._interval);
108+
cancelAnimationFrame(this._requestAnimation);
99109
}
100110
}
101111

0 commit comments

Comments
 (0)