亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

如何在定時器倒計時到 0 時觸發事件

如何在定時器倒計時到 0 時觸發事件

冉冉說 2023-03-10 13:53:00
我有一個父組件,里面有計時器組件。計時器從 15 分鐘開始倒計時直到 0。當我的計時器顯示時間為 0 時,我想觸發提交按鈕事件,提交按鈕在測驗組件內(測驗組件也是父組件的子組件)。我發現當 p 標簽改變時,我可能可以使用 MutationObserver。我不確定這是正確且唯一的方法還是有更好的方法來實現這一目標。父組件:import React, { Component } from 'react';import '../css/App.css'import Quiz from './Quiz';import Timer from './Timer';import { connect } from 'react-redux';import { ActionTypes } from '../redux/constants/actionTypes';import { saveQuizAll, getQuizIndex } from '../commonjs/common.js';const mapStateToProps = state => { return { ...state.quiz, ...state.quizAll } };const mapDispatchToProps = dispatch => ({  onQuizLoad: payload => dispatch({ type: ActionTypes.QuizLoad, payload }),  onQuizChange: payload => dispatch({ type: ActionTypes.QuizAnswerAll, payload }),  onPagerUpdate: payload => dispatch({ type: ActionTypes.PagerUpdate, payload })});class QuizContainer extends Component {  state = {    quizes: [      { id: 'data/class1.json', name: 'Class 1' },      { id: 'data/class2.json', name: 'Class 2' },      { id: 'data/class3.json', name: 'Class 3' },      { id: 'data/class4.json', name: 'Class 4' },    ],    quizId: 'data/class1.json'  };  pager = {    index: 0,    size: 1,    count: 1  }  componentDidMount() {    console.log('componentDidMount');    this.load(this.state.quizId);  }  load(quizId, isValReload) {    console.log('In load');    let url = quizId || this.props.quizId;    if (isValReload) {      let quiz = this.props.quizAll.find(a => url.indexOf(`${a.id}.`) !== -1);      console.log('In load quiz : ', quiz);      this.pager.count = quiz.questions.length / this.pager.size;      this.props.onQuizLoad(quiz);      this.props.onPagerUpdate(this.pager);    }
查看完整描述

3 回答

?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

我認為你可以有兩種方法。


1.“反應”方式

在父組件中:

// ...

constructor(props) {

  // ...

  this.state = {

    timeExpired: false

  };

}

    

const onTimeExpired = () => {

  this.setState({timeExpired: true});

}

   

// ...

    

render() {

  return (

    <div className="container">

      { // ... }

      <Timer duration={900} onTimeExpired={onTimeExpired}/>

      <Quiz quiz={this.state.quiz} quizId={this.state.quizId} saveAll={saveQuizAll} mode={this.state.mode} triggerSubmit={this.state.timeExpired} />

      </div>

    );

}

在定時器組件中:

// ...


componentDidUpdate() {

  if (this.state.seconds === this.props.duration) {

    this.props.onTimeExpired();

  }

}


// ...

在測驗組件中:

// ...


componentDidUpdate() {

  if (this.props.triggerSubmit) {

    // Do whatever you do on submit

  }

}


// ...

2.“快速而骯臟”的方式:

在定時器組件中

// ...


componentDidUpdate() {

  if (this.state.seconds === this.props.duration) {

    const quizForm = document.getElementById('quizFormId');

    quizForm && quizForm.submit();

  }

}


// ...


查看完整回答
反對 回復 2023-03-10
?
Cats萌萌

TA貢獻1805條經驗 獲得超9個贊

在您的 Timer 組件中提供一個 onTimeFinished 道具方法。然后在你的渲染函數中你可以添加

{ !(this.props.duration-this.state.seconds) && this.props.onTimeFinished() }

參考:反應條件渲染


查看完整回答
反對 回復 2023-03-10
?
UYOU

TA貢獻1878條經驗 獲得超4個贊

嘗試這個:


父組件:


// state

state = {

    triggerSubmit: false

}  


// functions

doSubmit = () => {

    this.setState({ triggerSubmit: true });

}

resetSubmit = () => {

    this.setState({ triggerSubmit: false });

}  


// jsx

<Timer duration={900} doSubmit={this.doSubmit} />

<Quiz 

    quiz={this.state.quiz}

    quizId={this.state.quizId}

    saveAll={saveQuizAll}

    mode={this.state.mode}

    resetSubmit={this.resetSubmit}

    triggerSubmit={this.state.triggerSubmit} />

計時器組件:


// function

doSubmit = (timeLeft) => {

  if (timeLeft === 0) {

    this.props.doSubmit();

  }

}  


// jsx

<p className="badge badge-success"

   onChange={() => {this.doSubmit(timeLeft)}>

   Time Left: {minutesDisplay}{secondsDisplay}

</p>

測驗組件:


// state

state = {

    triggerSubmit: this.props.triggerSubmit

}  


// function

triggerSubmit = () => {

    if (this.state.triggerSubmit) {

       your trigger submit code here...

       this.props.resetSubmit();

    }

}


查看完整回答
反對 回復 2023-03-10
  • 3 回答
  • 0 關注
  • 168 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號