This repository was archived by the owner on Dec 4, 2024. It is now read-only.
forked from ocetnik/react-native-background-timer
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBackgroundTimerExample.tsx
158 lines (154 loc) · 4.58 KB
/
BackgroundTimerExample.tsx
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import {View, Button, StyleSheet, Text,TextInput} from 'react-native';
import React, {useState} from 'react';
import BackgroundTimer from "react-native-background-timer";
export function BackgroundTimerExample() {
let [count, setCount] = useState(0);
let [text, setText] = useState("");
// BackgroundTimer延时
let [delay, setDelay] = useState("1000");
// setTimeout延时
let [timeoutDelay, setTimeoutDelay] = useState("1000");
// setInterval延时
let [intervalDelay, setIntervalDelay] = useState("1000");
let timeoutList:number[] = []
let [intervalList, setIntervalList] = useState<number[]>([]);
// runBackgroundTimer
function onPressStart(){
setText("开启定时器...")
BackgroundTimer.runBackgroundTimer(()=>{
setCount(count+=1)
},parseInt(delay))
}
function onPressStop(){
setText("结束定时器")
BackgroundTimer.stopBackgroundTimer()
}
// setTimeout
function setTimeoutStart(){
setText("开启定时器...")
let timeoutId = BackgroundTimer.setTimeout(()=>{
setCount(count+=1)
},parseInt(timeoutDelay))
timeoutList.push(timeoutId)
}
function setTimeoutStop(){
setText("结束定时器")
if(timeoutList.length>0){
BackgroundTimer.clearTimeout(timeoutList[0])
timeoutList.shift()
}
}
// setInterval
function setIntervalStart(){
setText("开启定时器...")
let intervalId = BackgroundTimer.setInterval(()=>{
setCount(count+=1)
},parseInt(intervalDelay))
setIntervalList([...intervalList,intervalId])
}
function setIntervalStop(){
setText("结束定时器")
if(intervalList.length>0){
BackgroundTimer.clearInterval(intervalList[0])
intervalList.shift()
}
}
function resetNumber(){
setCount(0)
setText("")
}
return (
<View style={{flexDirection: 'column', flex: 1,backgroundColor:'white'}}>
<View style={styles.container}>
<View style={styles.viewStyle}>
<Button
onPress={onPressStart}
title="runBackgroundTimer"
/>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
placeholder="BackgroundTimer延时"
onChangeText={(value)=>{setDelay(value)}}
value={delay}
/>
</View>
<View style={[styles.viewStyle]}>
<Button
onPress={onPressStop}
title="stopBackgroundTimer"
/>
</View>
</View>
{/* --------------------------------------------setTimeout-------------------------------------------------- */}
<View style={styles.container}>
<View style={styles.viewStyle}>
<Button
onPress={setTimeoutStart}
title="setTimeout"
/>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
placeholder="setTimeout延时"
onChangeText={(value)=>{setTimeoutDelay(value)}}
value={timeoutDelay}
/>
</View>
<View style={[styles.viewStyle]}>
<Button
onPress={setTimeoutStop}
title="clearTimeout"
/>
</View>
</View>
{/* --------------------------------------------setInterval-------------------------------------------------- */}
<View style={styles.container}>
<View style={styles.viewStyle}>
<Button
onPress={setIntervalStart}
title="setInterval"
/>
<TextInput
style={{ height: 40, borderColor: 'gray', borderWidth: 1 }}
placeholder="setInterval延时"
onChangeText={(value)=>{setIntervalDelay(value)}}
value={intervalDelay}
/>
</View>
<View style={[styles.viewStyle]}>
<Button
onPress={setIntervalStop}
title="clearInterval"
/>
</View>
</View>
{/* -----------------------------------------------Reset----------------------------------------------------- */}
<View style={[styles.viewStyle,styles.resetStyle]}>
<Button
onPress={resetNumber}
title="Reset"
/>
</View>
<Text style={styles.textStyle}>{count}</Text>
<Text style={styles.textStyle}>{text}</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
borderColor: 'black',
borderTopWidth: 0,
borderBottomWidth: 5,
borderLeftWidth: 0,
borderRightWidth: 0,
padding: 10,
},
resetStyle: {
paddingTop: 10,
},
viewStyle:{
marginBottom: 10,
},
textStyle: {
fontSize: 30,
},
});