-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.js
65 lines (59 loc) · 2.09 KB
/
Timer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React, {useEffect, useState} from 'react'
import {View, Text, Alert} from 'react-native'
import {fonts } from '../../constants/theme';
import { scale } from "react-native-size-matters";
import { useFocusEffect } from '@react-navigation/native';
const TimerView = ({duration, start=false, timeout_Function}) => {
const [countDownTime, setcountDownTime] = useState(duration)
useEffect(() => {
const intervalId = setInterval(() => {
if(start){
if(countDownTime > 0){
setcountDownTime(countDownTime => countDownTime-1)
}else{
setcountDownTime(0)
timeout_Function()
}
}
}, 1000)
return () => clearInterval(intervalId)
});
useFocusEffect(
React.useCallback(() => {
setcountDownTime(duration)
}, [duration])
);
const fancyTimeFormat = (duration) => {
var mins = ~~((duration % 3600) / 60);
var secs = ~~duration % 60;
var ret = "";
ret += "" + mins + " mins " + (secs < 10 ? "0" : "");
ret += "" + secs + " Secs ";
return ret;
}
return(
<View>
<Text
style ={{
color: '#4A90E2',
fontSize:scale(16),
fontFamily:fonts.Medium,
alignSelf: 'center',
marginVertical: 10,
}}>
{fancyTimeFormat(countDownTime)} remaining ...
</Text>
<Text
style ={{
color: '#4A90E2',
fontSize:scale(16),
fontFamily:fonts.Medium,
alignSelf: 'center',
marginVertical: 10,
}}>
{countDownTime} remaining ...
</Text>
</View>
)
}
export default TimerView