|
| 1 | +import {View, Button, StyleSheet, Text,TextInput} from 'react-native'; |
| 2 | +import React, {useState} from 'react'; |
| 3 | +import BackgroundTimer from "react-native-background-timer"; |
| 4 | +export function BackgroundTimerExample() { |
| 5 | + let [count, setCount] = useState(0); |
| 6 | + let [text, setText] = useState(""); |
| 7 | + // BackgroundTimer延时 |
| 8 | + let [delay, setDelay] = useState("1000"); |
| 9 | + // setTimeout延时 |
| 10 | + let [timeoutDelay, setTimeoutDelay] = useState("1000"); |
| 11 | + // setInterval延时 |
| 12 | + let [intervalDelay, setIntervalDelay] = useState("1000"); |
| 13 | + let timeoutList:number[] = [] |
| 14 | + let [intervalList, setIntervalList] = useState<number[]>([]); |
| 15 | + |
| 16 | + // runBackgroundTimer |
| 17 | + function onPressStart(){ |
| 18 | + setText("开启定时器...") |
| 19 | + BackgroundTimer.runBackgroundTimer(()=>{ |
| 20 | + setCount(count+=1) |
| 21 | + },parseInt(delay)) |
| 22 | + } |
| 23 | + function onPressStop(){ |
| 24 | + setText("结束定时器") |
| 25 | + BackgroundTimer.stopBackgroundTimer() |
| 26 | + } |
| 27 | + |
| 28 | + // setTimeout |
| 29 | + function setTimeoutStart(){ |
| 30 | + setText("开启定时器...") |
| 31 | + let timeoutId = BackgroundTimer.setTimeout(()=>{ |
| 32 | + setCount(count+=1) |
| 33 | + },parseInt(timeoutDelay)) |
| 34 | + timeoutList.push(timeoutId) |
| 35 | + } |
| 36 | + function setTimeoutStop(){ |
| 37 | + setText("结束定时器") |
| 38 | + if(timeoutList.length>0){ |
| 39 | + BackgroundTimer.clearTimeout(timeoutList[0]) |
| 40 | + timeoutList.shift() |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // setInterval |
| 45 | + function setIntervalStart(){ |
| 46 | + setText("开启定时器...") |
| 47 | + let intervalId = BackgroundTimer.setInterval(()=>{ |
| 48 | + setCount(count+=1) |
| 49 | + },parseInt(intervalDelay)) |
| 50 | + setIntervalList([...intervalList,intervalId]) |
| 51 | + } |
| 52 | + function setIntervalStop(){ |
| 53 | + setText("结束定时器") |
| 54 | + if(intervalList.length>0){ |
| 55 | + BackgroundTimer.clearInterval(intervalList[0]) |
| 56 | + intervalList.shift() |
| 57 | + } |
| 58 | + } |
| 59 | + function resetNumber(){ |
| 60 | + setCount(0) |
| 61 | + setText("") |
| 62 | + } |
| 63 | + return ( |
| 64 | + <View style={{flexDirection: 'column', flex: 1,backgroundColor:'white'}}> |
| 65 | + <View style={styles.container}> |
| 66 | + <View style={styles.viewStyle}> |
| 67 | + <Button |
| 68 | + onPress={onPressStart} |
| 69 | + title="runBackgroundTimer" |
| 70 | + /> |
| 71 | + <TextInput |
| 72 | + style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} |
| 73 | + placeholder="BackgroundTimer延时" |
| 74 | + onChangeText={(value)=>{setDelay(value)}} |
| 75 | + value={delay} |
| 76 | + /> |
| 77 | + </View> |
| 78 | + <View style={[styles.viewStyle]}> |
| 79 | + <Button |
| 80 | + onPress={onPressStop} |
| 81 | + title="stopBackgroundTimer" |
| 82 | + /> |
| 83 | + </View> |
| 84 | + </View> |
| 85 | + {/* --------------------------------------------setTimeout-------------------------------------------------- */} |
| 86 | + <View style={styles.container}> |
| 87 | + <View style={styles.viewStyle}> |
| 88 | + <Button |
| 89 | + onPress={setTimeoutStart} |
| 90 | + title="setTimeout" |
| 91 | + /> |
| 92 | + <TextInput |
| 93 | + style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} |
| 94 | + placeholder="setTimeout延时" |
| 95 | + onChangeText={(value)=>{setTimeoutDelay(value)}} |
| 96 | + value={timeoutDelay} |
| 97 | + /> |
| 98 | + </View> |
| 99 | + <View style={[styles.viewStyle]}> |
| 100 | + <Button |
| 101 | + onPress={setTimeoutStop} |
| 102 | + title="clearTimeout" |
| 103 | + /> |
| 104 | + </View> |
| 105 | + </View> |
| 106 | + {/* --------------------------------------------setInterval-------------------------------------------------- */} |
| 107 | + <View style={styles.container}> |
| 108 | + <View style={styles.viewStyle}> |
| 109 | + <Button |
| 110 | + onPress={setIntervalStart} |
| 111 | + title="setInterval" |
| 112 | + /> |
| 113 | + <TextInput |
| 114 | + style={{ height: 40, borderColor: 'gray', borderWidth: 1 }} |
| 115 | + placeholder="setInterval延时" |
| 116 | + onChangeText={(value)=>{setIntervalDelay(value)}} |
| 117 | + value={intervalDelay} |
| 118 | + /> |
| 119 | + </View> |
| 120 | + <View style={[styles.viewStyle]}> |
| 121 | + <Button |
| 122 | + onPress={setIntervalStop} |
| 123 | + title="clearInterval" |
| 124 | + /> |
| 125 | + </View> |
| 126 | + </View> |
| 127 | + {/* -----------------------------------------------Reset----------------------------------------------------- */} |
| 128 | + <View style={[styles.viewStyle,styles.resetStyle]}> |
| 129 | + <Button |
| 130 | + onPress={resetNumber} |
| 131 | + title="Reset" |
| 132 | + /> |
| 133 | + </View> |
| 134 | + <Text style={styles.textStyle}>{count}</Text> |
| 135 | + <Text style={styles.textStyle}>{text}</Text> |
| 136 | + </View> |
| 137 | + ); |
| 138 | +} |
| 139 | + |
| 140 | +const styles = StyleSheet.create({ |
| 141 | + container: { |
| 142 | + borderColor: 'black', |
| 143 | + borderTopWidth: 0, |
| 144 | + borderBottomWidth: 5, |
| 145 | + borderLeftWidth: 0, |
| 146 | + borderRightWidth: 0, |
| 147 | + padding: 10, |
| 148 | + }, |
| 149 | + resetStyle: { |
| 150 | + paddingTop: 10, |
| 151 | + }, |
| 152 | + viewStyle:{ |
| 153 | + marginBottom: 10, |
| 154 | + }, |
| 155 | + textStyle: { |
| 156 | + fontSize: 30, |
| 157 | + }, |
| 158 | +}); |
0 commit comments