简体   繁体   English

如何在此计时器中添加暂停功能? 反应母语

[英]How can i add a pause functionality in this timer ? React-Native

//Importing different files //导入不同的文件

import * as React from 'react';
import { Text, View, StyleSheet,Button } from 'react-native';
import { Constants } from 'expo';
import AssetExample from './components/AssetExample';

//Creating a stylesheet //创建样式表

const styles = StyleSheet.create({
  appContainer : {
    flex :1 ,
    alignItems : 'center',
    justifyContent : 'center',
  },
  text : {
    fontSize : 40 ,
  }
})

export class App extends React.Component{

//Creating a constructor function and setting state here //在此处创建构造函数并设置状态

  constructor(props)
  {
    super(props)
    this.state = ({
      minutes : 0,
      seconds : 4,
      pause : false,
  })
}

//Timer starting function for minutes and if it gets decreased by 1 then automatically set the seconds state. //计时器启动功能持续数分钟,如果将其减少1,则自动设置秒状态。

startTimer = () => {
  this.setState({
     minutes : this.state.minutes - 1,
    seconds : 4
    })
 }

//Timer2 for decreasing seconds by 1 every instant. // Timer2每秒钟将秒数减少1。

 startTimer2 = () => {
  this.setState({
    seconds : this.state.seconds - 1 

  })
  console.log(this.props.toggle)

 }

//Setting interval for both timers here. //在此设置两个计时器的间隔。

 componentDidMount = () => {
 this.interval = setInterval(this.startTimer , 5000)
   this.interval2 = setInterval(this.startTimer2 , 1000) 
 }

//Don't update if minutes gets below zero. //如果分钟数小于零,请勿更新。

 shouldComponentUpdate = (nextProps , nextState) => {
 if(nextState.minutes >= 0 ) 
 {
 return( 

true
 )
 }

// JUST clear INterval if it gets below //如果低于以下值,则只需清除INTERVAL

 else {
  clearInterval(this.interval)
  clearInterval(this.interval2)
   }
   }

//Component will get unmount //组件将被卸载

   componentWillUnmount = () => {
   clearInterval(this.interval)
   clearInterval(this.interval2)
   }

//Toggler for pausing the time //切换暂停时间

   pauseTimer = ()=> {
                  this.setState({
                      pause : !this.state.pause
                                               })
                                   }

//A render function //一个渲染函数

         render() {

                  if(this.state.minutes === 0  && this.state.seconds === 0)
 {
     return(
      <View style = {styles.appContainer}>
  <Text style ={styles.text}>
  Break is over
  </Text>

  </View>
    )

    }
     else if (this.state.pause === false)
     {

     return (
     <View style = {styles.appContainer}>
     <Text style ={styles.text}>
     {this.state.minutes} : {this.state.seconds}

      </Text>
      <Button title = "Pause" onPress = {()=>this.pauseTimer()} 
      />

      </View>
      )
      }

    else {

     return (
     <View style = {styles.appContainer}>
     <Text style ={styles.text}>
      {this.state.minutes} : {this.state.seconds}
                Paused
          </Text>
      <Button title = "Start again" onPress = {()=>this.pauseTimer()}/>
     </View>
         )

       }

      }

I have created a short snippet in pure javascript, showing how to stop and restart an interval. 我已经用纯JavaScript创建了一个简短的代码段,展示了如何停止和重新启动间隔。

It also shows how you can calculate minutes and seconds with one counter. 它还显示了如何使用一个计数器来计算分钟和秒。

Sorry, but I have not implemented this in React! 抱歉,但是我还没有在React中实现它! You still have to implement this in your React template, but the setInterval logic and the seconds counter should work the same. 您仍然必须在React模板中实现这一点,但是setInterval逻辑和秒计数器应该工作相同。

let id;
let paused = true;
let seconds = 0;
let btn = document.querySelector("#btn");

btn.addEventListener("click", ()=>{
  toggleTimer();
});

function startTimer(){
  id = setInterval(()=>{
    seconds++;
    let minutes = Math.floor(seconds/60);
    let sec = seconds%60;
    console.log(minutes + " : " + sec);
  }, 100);
}

function stopTimer(){
  clearInterval(id);
}

function toggleTimer(){
  console.log(paused);
  if(paused){
    paused = false;
    startTimer();
  } else {
    paused = true;
    stopTimer();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM