「開始」ボタンをクリックするか、タイマーが停止したときに入力フィールドが無効になり、誰も入力できない状態になると、状態を無効に設定しますそれは再び "スタート"ボタンをクリックすると、テキストフィールドが再び有効になるように正しく動作しませんでした。私はそれを無効にするか、タイマーが停止した後にのみ有効にする必要があります。反応の開始ボタンでテキストフィールドを無効にする
コード:startTimer
上
<script type="text/jsx">
var styles = {
margin: '2em auto',
width: '300px',
height: '300px',
backgroundColor: '#DD4814',
color: '#ffffff',
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'space-around'
};
var inputs = {
position: 'relative',
bottom: '17%',
left: '20%'
}
var btns = {
position: 'relative',
bottom: '7%'
}
var btn = {
backgroundColor: '#ffffff',
color: '#000000',
borderColor: '#DEB887',
borderRadius: '0.4em',
cursor: 'pointer',
margin: '0 1em',
padding: '0.5em',
display: 'inline-block'
}
var required = true;
class Timer extends React.Component {
constructor (props) {
super(props)
this.state = {count: 0, customNumber: 0}
}
handleChange (e) {
this.setState({ customNumber: e.target.value});
}
componentWillUnmount() {
clearInterval(this.timer)
}
tick() {
if (this.state.customNumber) {
this.setState({
count: (this.state.customNumber--)
})
if (this.state.customNumber <= 0) {
this.setState({ count: 0})
clearInterval(this.timer)
this.setState({disabled: !this.state.disabled})
}
} else {
this.setState({count: (this.state.count - 1)})
}
}
display() {
return ('0' + this.state.count % 100).slice(-2)
}
startTimer() {
clearInterval(this.timer)
this.timer = setInterval(this.tick.bind(this), 1000)
this.setState({disabled: !this.state.disabled})
}
stopTimer() {
clearInterval(this.timer)
}
resetTimer() {
clearInterval(this.timer)
this.setState({count: 0})
this.setState({disabled: !this.state.disabled})
}
render() {
return (
<div style={styles} className='timer'>
<h1 style={{fontSize: '4em'}}>{this.display()}</h1>
<div className="input_text" style={inputs}>
<label htmlFor="custom_number">Enter number to start timer</label>
<input type="text" name="custom_number" id="custom_number" required={required} value={this.state.inputValue} onChange={this.handleChange.bind(this)} disabled={this.state.disabled} placeholder="Enter b/w 1-100" />
</div>
<div style={btns} className="buttons">
<button style={btn} type="button" name="start_btn" id="start_btn" onClick={this.startTimer.bind(this)}>Start</button>
<button style={btn} type="button" name="stop_btn" id="stop_btn" onClick={this.stopTimer.bind(this)}>Pause</button>
<button style={btn} type="button" name="reset_btn" id="reset_btn" onClick={this.resetTimer.bind(this)}>Stop</button>
</div>
</div>
)
}
}
ReactDOM.render(
<Timer />,
document.getElementById('root')
)
</script>
<div id="root"></div>
Thanknsそれが動作し、あなたが私は私の入力に5を入れた場合、それは5,4,3のように行く...なぜカウンターが1にカウントされませんない私に言うことができ、不要な状態を持っています、2,0。 –
なぜ 'customNumber'と' count'が必要ですか?彼らは同じではないと思いますか? – mersocarlin
私は間違っているところで修正することができますか?私はそれを作るためにインターネットから反応してコピーした部分を完全に新しくしました。 –