Я пытаюсь уменьшить мою собственность timeLeft
на моем штате один раз в секунду. В функции startRecording
я вызываю метод startTimer
, но, похоже, не может уменьшить состояние. Что я делаю не так? Благодаря!Уменьшение числа в государстве-ответчике
class NimbusCamera extends Component {
static propTypes = {
dispatch: PropTypes.func.isRequired,
navigator: PropTypes.object.isRequired
}
state = {
camera: {
aspect: Camera.constants.Aspect.fill,
captureTarget: Camera.constants.CaptureTarget.disk,
type: Camera.constants.Type.front,
captureMode: Camera.constants.CaptureMode.video,
captureAudio: true,
flashMode: Camera.constants.FlashMode.auto
},
isRecording: false,
timeLeft: 30,
reachedLimit: false
}
startTimer =() => {
console.log('Starting timer...')
let timerId = setInterval(countdown, 1000); // Run function every second
const countdown =() => {
console.log('Counting down...')
if (this.state.timeLeft === 0) {
clearTimeout(timerId);
this.setState({isRecording: false})
} else {
console.log(Decrementing...)
this.setState({timeLeft: this.state.timeLeft - 1});
}
}
}
render() {
console.log(this.state)
return (
<View style={styles.container}>
<Camera
ref={(cam) => {
this.camera = cam;
}}
style={styles.preview}
aspect={this.state.camera.aspect}
type={this.state.camera.type}
captureAudio={this.state.camera.captureAudio}
flashMode={this.state.camera.flashMode}
>
<Text>{this.state.timeLeft}</Text>
<Text style={styles.capture} onPress={this.startRecording.bind(this)}>[CAPTURE]</Text>
<Text style={styles.capture} onPress={this.stopRecording.bind(this)}>[STOP]</Text>
</Camera>
</View>
);
}
startRecording =() => {
if (this.camera) {
this.camera.capture({mode: Camera.constants.CaptureMode.video})
.then((data) => this.props.dispatch(getPath(data.path)))
.catch(err => console.error(err));
this.startTimer();
this.setState({
isRecording: true
});
}
}
stopRecording =() => {
if (this.camera) {
this.camera.stopCapture();
this.setState({
isRecording: false
});
}
}
}
Вы вызываете 'setInterval' в' countdown' перед объявлением 'countdown', я не уверен, но так как это локальная функция, то первый оператор' let timerId = setInterval (undefined, 1000); ' – niceman
@niceman Я попробовал поместить его в функцию обратного отсчета, но ничего не сделал. – maxwellgover
Я не предлагал поместить его в обратный отсчет, это было бы очень неправильно, я предложил поместить его после обратного отсчета, то есть после фигурной скобки, которая заканчивается функцией 'countdown' – niceman