Skip to content

Commit 996c46f

Browse files
committed
reduces long-ass animation duration
1 parent f7134de commit 996c46f

File tree

2 files changed

+14
-19
lines changed

2 files changed

+14
-19
lines changed

web-server/src/content/DoraMetrics/DoraCards/ChangeFailureRateCard.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,7 @@ export const ChangeFailureRateCard = () => {
8181
.incident_count
8282
);
8383

84-
const changeFailureRateCount = useCountUp(
85-
changeFailureRateProps.count || 0,
86-
1500, // animation duration
87-
2 // decimal place
88-
);
84+
const changeFailureRateCount = useCountUp(changeFailureRateProps.count || 0);
8985

9086
const series = useMemo(
9187
() => [

web-server/src/hooks/useCountUp.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
11
import { useState, useEffect } from 'react';
22

3-
const FRAME_DURATION_MS = 16; // Average frame duration for 60fps
3+
const STEPS = 7; // number of steps to reach the target value
4+
const INTERVAL = 75; // in ms
45

56
export const useCountUp = (
67
targetValue: number,
7-
duration: number = 1500,
88
decimalPlaces: number = 0
99
): number => {
1010
const [count, setCount] = useState<number>(0);
1111

1212
useEffect(() => {
13-
let start = 0;
14-
const increment = targetValue / (duration / FRAME_DURATION_MS);
13+
let currentStep = 0;
14+
const stepValue = targetValue / STEPS;
1515

16-
const animateCount = () => {
17-
start += increment;
16+
const timer = setInterval(() => {
17+
currentStep++;
1818

19-
if (start >= targetValue) {
19+
if (currentStep >= STEPS) {
2020
setCount(parseFloat(targetValue.toFixed(decimalPlaces)));
21+
clearInterval(timer);
2122
} else {
22-
setCount(parseFloat(start.toFixed(decimalPlaces)));
23-
requestAnimationFrame(animateCount);
23+
const newValue = stepValue * currentStep;
24+
setCount(parseFloat(newValue.toFixed(decimalPlaces)));
2425
}
25-
};
26+
}, INTERVAL);
2627

27-
animateCount();
28-
29-
return () => {};
30-
}, [targetValue, duration, decimalPlaces]);
28+
return () => clearInterval(timer);
29+
}, [targetValue, decimalPlaces]);
3130

3231
return count;
3332
};

0 commit comments

Comments
 (0)