TimerLable.js
2.5 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
import React from 'react';
import Immutable, {Map} from 'immutable';
import YH_Image from '../../../common/components/YH_Image';
import TimerMixin from 'react-timer-mixin';
import ReactNative, {
View,
Text,
Image,
StyleSheet,
Dimensions,
TouchableOpacity,
Platform,
} from 'react-native';
export default class TimerLable extends React.Component {
constructor(props) {
super(props);
this.state = {
leftTime: this.props.leftTime,
}
}
componentDidMount() {
this.startTimer();
}
componentWillUnmount() {
this.stopTimer();
}
startTimer() {
this.stopTimer();
if(this.state.leftTime > 0){
this.timer = TimerMixin.setInterval(() => {
let leftTime = this.state.leftTime-1;
if (leftTime < 0) {
this.props.onStop && this.props.onStop()
this.stopTimer();
}
this.setState({
leftTime,
});
}, 1000);
}else {
this.setState({
leftTime: 0,
});
}
}
stopTimer() {
console.log('stopTimer');
this.timer && TimerMixin.clearTimeout(this.timer);
}
format(m) {
if (m < 10){
return 0+''+m;
}
return m+'';
}
formatTime(shijianchuo) {
// 秒数
var second = Math.floor(shijianchuo);
// 小时位
var hr = Math.floor(second / 3600);
var hrFormat = this.format(hr);
// 分钟位
var min = Math.floor((second - hr * 3600) / 60);
var minFormat = this.format(min);
// 秒位
var sec = (second - hr * 3600 - min * 60);
var secFormat = this.format(sec);
return hrFormat + ':' + minFormat + ':' + secFormat;
}
render() {
if(this.state.leftTime > 0){
return (
<View style={styles.container}>
<Text style={[styles.time,this.props.style ? this.props.style : {}]}>
{this.formatTime(this.state.leftTime)}
</Text>
</View>
);
} else {
return null;
}
}
}
let {width, height} = Dimensions.get('window');
const DEVICE_WIDTH_RATIO = width / 375;
let leftOffset = 20 * DEVICE_WIDTH_RATIO;
let styles = StyleSheet.create({
container: {
flexDirection: 'row',
alignItems: 'center',
},
time: {
fontFamily: 'PingFang-SC-Medium',
fontSize: 12,
color: '#ffffff',
letterSpacing: 0,
marginLeft: 5,
},
});