最近中文字幕高清中文字幕无,亚洲欧美高清一区二区三区,一本色道无码道dvd在线观看 ,一个人看的www免费高清中文字幕

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 React 中的特定函數(shù)上添加延遲?

如何在 React 中的特定函數(shù)上添加延遲?

喵喔喔 2023-08-18 16:21:46
我構(gòu)建了一個石頭/剪刀/布 React 應(yīng)用程序。該應(yīng)用程序工作正常,我只是想在 CPU 必須選擇武器(石頭/布/剪刀)時添加一些延遲。我希望有一個時間窗口,CPU 選擇的圖像不會出現(xiàn)在屏幕上,而用戶的選擇會立即出現(xiàn)在屏幕上。我嘗試在 compounentDidMount() 方法中添加 setInterval() 函數(shù),但沒有成功。如何僅在 CPU 部分添加延遲?https://codesandbox.io/s/nice-ardinghelli-96sum?file=/src/components/Main.js預(yù)先非常感謝您。
查看完整描述

4 回答

?
慕絲7291255

TA貢獻(xiàn)1859條經(jīng)驗 獲得超6個贊

您需要添加一個不渲染任何內(nèi)容的狀態(tài)。例如,只需將 cpu 選擇設(shè)置為 4 并更新渲染函數(shù)。然后你可以像這里一樣添加睡眠功能。我做了一些我認(rèn)為你想要的工作示例
我對你的代碼所做的主要更改是

this.sleep(1500).then(() => {

? ? ? const index = getRandomInt(3);

? ? ? this.setState({

? ? ? ? houseChoice: index

? ? ? });

? ? ? const results = this.getResults(choiceName, index).toUpperCase();

? ? ? this.setState({

? ? ? ? results: results

? ? ? });


? ? ? /*****************calling setScore()********************/

? ? ? if (results === "WIN") {

? ? ? ? this.props.setScore(1);

? ? ? } else if (results === "LOSE") {

? ? ? ? this.props.setScore(-1);

? ? ? } else {

? ? ? ? this.props.setScore(0);

? ? ? }

? ? });

其中handleClick執(zhí)行超時。

在結(jié)果頁面中我添加了


this.state.houseChoice === 2 ? (

? ? ? ? ? ? ?/*3*/

? ? ? ? ? ? ? <div

? ? ? ? ? ? ? ? className="elem-container result-container"

? ? ? ? ? ? ? ? style={{

? ? ? ? ? ? ? ? ? borderColor: "hsl(349, 71%, 52%)",

? ? ? ? ? ? ? ? ? color: "hsl(349, 70%, 56%)"

? ? ? ? ? ? ? ? }}

? ? ? ? ? ? ? >

? ? ? ? ? ? ? ? <img src={rock} className="choice-image" alt="img" />

? ? ? ? ? ? ? </div>

? ? ? ? ? ? ) : null


查看完整回答
反對 回復(fù) 2023-08-18
?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗 獲得超13個贊

人類玩家選擇一步棋后,不要立即讓 CPU 選擇一步棋。實現(xiàn)componentDidUpdate生命周期函數(shù)并將CPU的選擇邏輯移到那里。這也需要移動對獲勝邏輯的檢查。


/*function that handles the user choice*/

handleClick = (

  choiceName,

  choiceImage,

  choiceBorderColor,

  choiceExtraBorderColor

) => () => {

  this.setState({

    onScreen: false,

    choiceName,

    choiceImage,

    choiceBorderColor,

    choiceExtraBorderColor

  });

};

一旦人類玩家選擇了一個動作,就獲得房子的隨機(jī)動作,但不要立即將狀態(tài)更新排入隊列,而是稍后使用進(jìn)入setTimeout更新狀態(tài)。


僅當(dāng)兩個玩家都選擇了移動但結(jié)果尚未計算并存儲在狀態(tài)中時才檢查勝利。


componentDidUpdate(prevProps, prevState) {

  if (

    prevState.choiceName !== this.state.choiceName &&

    this.state.choiceName

  ) {

    function getRandomInt(max) {

      return Math.floor(Math.random() * Math.floor(max));

    }

    const index = getRandomInt(3);


    setTimeout(() => {

      this.setState({

        houseChoice: index

      });

    }, 2000);

  }


  if (

    this.state.choiceName &&

    [0, 1, 2].includes(this.state.houseChoice) && // We want 0 to be truthy :)

    !this.state.results

  ) {

    const results = this.getResults(

      this.state.choiceName,

      this.state.houseChoice

    ).toUpperCase();

    this.setState({

      results: results

    });


    /*****************calling setScore()********************/

    if (results === "WIN") {

      this.props.setScore(1);

    } else if (results === "LOSE") {

      this.props.setScore(-1);

    } else {

      this.props.setScore(0);

    }

  }

}

有條件地渲染“等待 CPU”UI


<h4 className="result-title">

  {this.state.houseChoice === ""

    ? "THE HOUSE CHOOSING"

    : "THE HOUSE PICKED"}

</h4>


{this.state.houseChoice === "" ? (

  <div>...</div>

) : this.state.houseChoice === 0 ? (

  /*1*/

  <div

    className="elem-container result-container"

    style={{

      borderColor: "hsl(230, 89%, 62%)",

      color: "hsl(230, 89%, 65%)"

    }}

  >

    <img src={paper} className="choice-image" alt="img" />

  </div>

) : this.state.houseChoice === 1 ? (

  /*2*/

  <div

    className="elem-container result-container"

    style={{

      borderColor: "hsl(39, 89%, 49%)",

      color: "hsl(40, 84%, 53%)"

    }}

  >

    <img src={scissors} className="choice-image" alt="img" />

  </div>

) : (

  /*3*/

  <div

    className="elem-container result-container"

    style={{

      borderColor: "hsl(349, 71%, 52%)",

      color: "hsl(349, 70%, 56%)"

    }}

  >

    <img src={rock} className="choice-image" alt="img" />

  </div>

)}

有條件地渲染結(jié)果并重試按鈕。


<div className="final-result-container">

  {this.state.results && (

    <>

      <h1 className="bold">YOU {this.state.results}</h1>

      <TryAgain onClick={this.handleTryAgainClick} />

    </>

  )}

</div>

再次玩時不要忘記重置所有狀態(tài)


handleTryAgainClick() {

  this.setState({

    onScreen: true,

    choiceName: "",

    choiceImage: "",

    choiceBorderColor: "",

    choiceExtraBorderColor: "",

    houseChoice: "",

    results: ""

  });

}

https://img1.sycdn.imooc.com//64df2a5c0001f9bc06380666.jpg

https://img1.sycdn.imooc.com//64df2a63000133d706400661.jpg

查看完整回答
反對 回復(fù) 2023-08-18
?
嚕嚕噠

TA貢獻(xiàn)1784條經(jīng)驗 獲得超7個贊

我不確定我的問題是否正確,但這里是,如果要在渲染上設(shè)置延遲,那么在做出選擇之前不要渲染。如果應(yīng)用程序必須在玩家輸入后執(zhí)行某些操作,請使用 async wait 異步執(zhí)行以下操作。



查看完整回答
反對 回復(fù) 2023-08-18
?
忽然笑

TA貢獻(xiàn)1806條經(jīng)驗 獲得超5個贊

如果我猜對了,也許 lodash 的 Debounce 會為你提供答案。我在我的井字游戲應(yīng)用程序中使用它來延遲計算機(jī)移動。只需從 NPM 添加即可

npm install lodash —save-dev

然后導(dǎo)入到你的文件中

import {debounce} from 'lodash';

不斷創(chuàng)建將去抖的內(nèi)容,您從 lodash 中調(diào)用了它。500 是時間,您想要移動延遲多少時間。在這種情況下,它將是 0.5 秒。

const debounceComputerMove = debounce(()=>{
  computerMove();
},500);

然后當(dāng)你想調(diào)用computerMove函數(shù)時,調(diào)用debounceComputerMove而不是computerMove()


查看完整回答
反對 回復(fù) 2023-08-18
  • 4 回答
  • 0 關(guān)注
  • 263 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號