簡體   English   中英

React:如何強制一個函數在另一個函數完全完成后運行?

[英]React: How to force a function to run after another one is totally completed?

在這個 React Pomodoro Clock 中,有一個函數countDown 其中有一個名為three的函數。 目前,當this.setState({ init: 'break' }); 設置為twothree立即發生。 但是, three應該等到two完成。 任何幫助將不勝感激。

索引.js

import React from 'react';
import ReactDOM from 'react-dom';
import './style.css';

/*
* A simple React component
*/
const initState = {
  breakLength: 5,
  breakSeconds: undefined,
  sessionLength: 25,
  sessionSeconds: undefined,
  init: 'session',
  timeLeft: undefined,
  timeLeftSeconds: undefined,
  started: false,
  intervalFunc: undefined
}

const secondsToMins = (time) => {
  //let converted = Math.floor(time / 60) + ':' + ('0' + Math.floor(time % 60)).slice(-2);
  let converted = ('0' + Math.floor(time / 60)).slice(-2) + ':' + ('0' + Math.floor(time % 60)).slice(-2);

  //console.log('converted')
  //console.log(converted)
  //console.log('#######')

  return converted;
}

class Clock extends React.Component {

  constructor(props) {
    super(props);
    this.state = initState;
    this.breakDecrement = this.breakDecrement.bind(this);
    this.breakIncrement = this.breakIncrement.bind(this);
    this.sessionDecrement = this.sessionDecrement.bind(this);
    this.sessionIncrement = this.sessionIncrement.bind(this);
    this.startStop = this.startStop.bind(this);
    this.reset = this.reset.bind(this);
  }

  componentDidMount() {
    // seconds are used for the countDown()
    // seconds are converted to MM:SS at every t-minus
    let breakSeconds = this.state.breakLength * 60;
    let sessionSeconds = this.state.sessionLength * 60;

    // Initialize everything
    this.setState({ breakSeconds: breakSeconds });
    this.setState({ sessionSeconds: sessionSeconds });
    this.setState({ timeLeftSeconds: sessionSeconds });
    this.setState({ timeLeft: secondsToMins(sessionSeconds) });

  }

  breakDecrement() {
    // decrements the breakLength and the breakSeconds
    // breakLength is only a number ie. 5 (does not show seconds)
    // breakSeconds is that nunber converted into seconds
    let breakLength = this.state.breakLength - 1;
    if (breakLength > 0 && breakLength < 61){
      this.setState({ breakLength: breakLength });
      let breakSeconds = breakLength * 60;
      this.setState({ breakSeconds: breakSeconds });
    }
  }

  breakIncrement() {
    // same as decrement except does increment
    let breakLength = this.state.breakLength + 1;
    if (breakLength > 0 && breakLength < 61){
      this.setState({ breakLength: breakLength });
      let breakSeconds = breakLength * 60;
      this.setState({ breakSeconds: breakSeconds });
    }
  }

  sessionDecrement() {
    // decrements the sessionLength and the sessionSeconds
    // sessionLength is only a number ie. 25 (does not show seconds)
    // sessionSeconds is that nunber converted into seconds
    let sessionLength = this.state.sessionLength - 1;
    if (sessionLength > 0 && sessionLength < 61){
      this.setState(prevState => ({
        sessionLength: prevState.sessionLength-1,
        sessionSeconds: (prevState.sessionLength-1)*60,
        timeLeftSeconds: (prevState.sessionLength-1)*60,
        timeLeft:  secondsToMins((prevState.sessionLength-1)*60)})
      );
    }
  }

  sessionIncrement() {
    // same as decrement except does increment
    let sessionLength = this.state.sessionLength + 1;
    if (sessionLength > 0 && sessionLength < 61){
      this.setState(prevState => ({
        sessionLength: prevState.sessionLength+1,
        sessionSeconds: (prevState.sessionLength+1)*60,
        timeLeftSeconds: (prevState.sessionLength+1)*60,
        timeLeft:  secondsToMins((prevState.sessionLength+1)*60)})
      );
    }
  }

  startStop(id) {
    // starts the countDown, which runs continuously until the start/stop button
    // is pressed again, which pauses the countdown.
    // the id parameter is used by countDown to play the audio beep
    if(!this.state.started){
      this.countDown(id);
      this.setState({ started: true});
    }
    // pauses the countDown
    if(this.state.started){
      let intervalFunc = this.state.intervalFunc;
      clearInterval(intervalFunc);
      this.setState({ started: false});
    }
  }

  reset() {
    let intervalFunc = this.state.intervalFunc;
    clearInterval(intervalFunc);
    // reset state to default values
    this.setState({ breakLength: 5 });
    this.setState({ sessionLength: 25 });
    this.setState({ init: 'session' });
    this.setState({ timeLeftSeconds: 1500})
    this.setState({ timeLeft: '25:00' });
  }

  countDown(id){
    // set the function to a variable and set state to it, so the function
    // can be paused with clearInterval()
    var intervalFunc = setInterval(() => down(this.state.timeLeftSeconds--), 1000);
    this.setState({intervalFunc: intervalFunc});

    const down = (time) =>
    {

      const one = async () =>{
        if(time > 0){
          // converts seconds to MM:SS at every t-minus
          this.setState({ timeLeft: secondsToMins(time)});
          console.log(time);
          console.log(this.state.timeLeft);
        }
        return;
      }

      //when time reaches 0 set state.init to break
      // and set seconds left to break seconds
      const two = async () => {
        if(time == 0 && this.state.init == 'session'){
          this.setState({ timeLeft: secondsToMins(time)});
          let sound = document.getElementById(id).childNodes[0];
          this.setState({ init: 'break' });
          console.log(this.state.init)
          sound.play();
          this.setState({ timeLeftSeconds: this.state.breakSeconds});
        }
        return;
      }

      //when time reaches 0 set state.init to session
      // and set seconds left to session seconds
      const three = async () => {
        if(time == 0 && this.state.init == 'break'){
          this.setState({ timeLeft: secondsToMins(time)});
          let sound = document.getElementById(id).childNodes[0];
          this.setState({ init: 'session' });
          console.log(this.state.init)
          sound.play();
          this.setState({ timeLeftSeconds: this.state.sessionSeconds});
        }
        return;
      }

      one().then(two).then(three);
    }
  }

  render() {
    return (
      <div id="clock">
      <h1 id="title">25-5 Clock</h1>

      <div>
      <p id="break-label">Break Length</p>
      <p id="break-length">{this.state.breakLength}</p>
      <button id="break-decrement" onClick={e => this.breakDecrement()}> Decrease </button>
      <button id="break-increment" onClick={e => this.breakIncrement()}> Increase </button>
      </div>

      <div>
      <p id="session-label">Session Length</p>
      <p id="session-length">{this.state.sessionLength}</p>
      <button id="session-decrement" onClick={e => this.sessionDecrement()}> Decrease </button>
      <button id="session-increment" onClick={e => this.sessionIncrement()}> Increase </button>
      </div>

      <hr/>

      <div>
      <p id="timer-label">{this.state.init}</p>
      <p id="time-left">{this.state.timeLeft}</p>
      <button id="start_stop" onClick={e => this.startStop(e.target.id)}><audio id="beep" src='./beep.mp3'></audio> start/stop </button>
      <button id="reset" onClick={e => this.reset()}> reset </button>
      </div>

      </div>
    );
  }
};

/*
* Render the above component into the div#app
*/
ReactDOM.render(<Clock />, document.getElementById("app"));

索引.html:

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <meta charset="utf-8">
  <title>25-5 Clock</title>
  <style>
  </style>
</head>
<body>
  <main>
    <div id="app"></app>
    </main>
  </body>
  </html>

您需要使它們異步。

喜歡...

const one = async () => {
  //some code
return
}
const two = async () => {
  //some code
return
}
const three = async () => {
  //some code
return
}

然后你可以...

one().then(two).then(three)

您可以通過使函數返回Promise並使用async / await關鍵字等待它們完成后再開始下一個來做到這一點。

 const setDelay = delay => new Promise(resolve => { console.log(`Process running for ${delay}`); setTimeout(() => { console.log('Process done'); resolve(); }, delay); }); (async () => { await setDelay(2000); await setDelay(3000); await setDelay(1000); })();

或者你可以不用async / await並鏈接承諾。

 const setDelay = delay => new Promise(resolve => { console.log(`Process running for ${delay}`); setTimeout(() => { console.log('Process done'); resolve(); }, delay); }); setDelay(3000) .then(() => setDelay(1000)) .then(() => setDelay(4000));

或者只是使用好的“老式回調”。 但我會選擇上述之一。

解決方案:

使用數組來保存狀態和持續時間:

const states = [ { name: 'session', duration: 1500 }, { name: 'break', duration: 300 } ]

交替數組的索引以在 session 和 break 之間交替。

countDown(id){
    // set the function to a variable and set state to it, so the function
    // can be paused with clearInterval()
    var intervalFunc = setInterval(() => down(this.state.timeLeftSeconds--), 1000);
    this.setState({intervalFunc: intervalFunc});

    const down = (time) =>
    {

      if(time > 0){
        // converts seconds to MM:SS at every t-minus
        this.setState({ timeLeft: secondsToMins(time)});
        console.log(time);
        console.log(this.state.timeLeft);
      }

      if (time <= 0) {
        this.setState({ timeLeft: secondsToMins(time)});
        let sound = document.getElementById(id).childNodes[0];
        sound.play();

        let stateIndex = (this.state.stateIndex + 1) % states.length;
        this.setState({ stateIndex: stateIndex});
        this.setState({ timeLeftSeconds: states[stateIndex].duration});
        this.setState({ init: states[stateIndex].name});
        console.log(this.state.init);
      }
    }
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM