diff --git a/zh-cn/react-native-background-timer.md b/zh-cn/react-native-background-timer.md new file mode 100644 index 0000000000000000000000000000000000000000..ccfc6d6136fa2249a3be08d3c6c78858a1d0afcd --- /dev/null +++ b/zh-cn/react-native-background-timer.md @@ -0,0 +1,344 @@ + +> 模板版本:v0.2.0 + +

+

react-native-background-timer

+

+

+ + Supported platforms + + + License + + +

+ +> [!TIP] [Github 地址](https://github.com/ocetnik/react-native-background-timer) + + +## 安装与使用 + +请到三方库的 Releases 发布地址查看配套的版本信息:[@react-native-oh-tpl/react-native-background-timer Releases](https://github.com/react-native-oh-library/react-native-background-timer/releases),并下载适用版本的 tgz 包。 + +进入到工程目录并输入以下命令: + +> [!TIP] # 处替换为 tgz 包的路径 + + + +#### **npm** + +```bash +npm install @react-native-oh-tpl/react-native-background-timer@file:# +``` + +#### **yarn** + +```bash +yarn add @react-native-oh-tpl/react-native-background-timer@file:# +``` + + + +下面的代码展示了这个库的基本使用场景: + +> [!WARNING] 使用时 import 的库名不变。 + +```js +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 ( + + + +