diff --git a/BackgroundTimerExample.tsx b/BackgroundTimerExample.tsx new file mode 100644 index 0000000000000000000000000000000000000000..770e7e9759d586669828d00849c4a3c5b61a8fd8 --- /dev/null +++ b/BackgroundTimerExample.tsx @@ -0,0 +1,184 @@ +/* +* +* MIT License +* +* Copyright (c) 2024 Huawei Device Co., Ltd. +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: + +* The above copyright notice and this permission notice shall be included in all +* copies or substantial portions of the Software. + +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +* SOFTWARE. +*/ + + +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([]); + + // 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 ( + + + +