From 7b29d888847a157132c9265c34de9c35886c64a5 Mon Sep 17 00:00:00 2001 From: xielei Date: Mon, 13 May 2024 17:52:09 +0800 Subject: [PATCH] =?UTF-8?q?[Issues:=20#I9OAHY]=20=E6=96=B0=E5=A2=9Ereact-n?= =?UTF-8?q?ative-background-timer=E6=8C=87=E5=AF=BC=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- zh-cn/react-native-background-timer.md | 344 +++++++++++++++++++++++++ 1 file changed, 344 insertions(+) create mode 100644 zh-cn/react-native-background-timer.md diff --git a/zh-cn/react-native-background-timer.md b/zh-cn/react-native-background-timer.md new file mode 100644 index 00000000..ccfc6d61 --- /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 ( + + + +