Javascript 如何在js中使用两个不同的间隔设置组件动画
我需要在名为character的组件上设置嘴巴的动画。 我想做的是使用名为openned的状态来处理我的口腔状态 我需要嘴巴动画每2秒运行一次,以允许角色间歇说话。这样做的目的是在下面显示文本,只有当文本出现时,嘴巴才能发出嘟嘟声Javascript 如何在js中使用两个不同的间隔设置组件动画,javascript,reactjs,jquery-animate,state,setinterval,Javascript,Reactjs,Jquery Animate,State,Setinterval,我需要在名为character的组件上设置嘴巴的动画。 我想做的是使用名为openned的状态来处理我的口腔状态 我需要嘴巴动画每2秒运行一次,以允许角色间歇说话。这样做的目的是在下面显示文本,只有当文本出现时,嘴巴才能发出嘟嘟声 state = { openned : false } handleMouthState = () => { this.setState({ openned : ! this.state.openned }); } animeMouth
state = {
openned : false
}
handleMouthState = () => {
this.setState({
openned : ! this.state.openned
});
}
animeMouth = () => {
setInterval(this.handleMouthState.bind(this), 100);
}
animMouthWithInterval = () => {
setInterval( this.animeMouth.bind(this), 2000 );
}
componentDidMount() {
setTimeout( this.animMouthWithInterval.bind(this) , 6000);
}
这是我尝试过的代码,它工作得很好,除了animMouth func继续运行,即使以2秒的间隔调用它,我除了停止动画然后重新加载它之外
state = {
openned : false
}
handleMouthState = () => {
this.setState({
openned : ! this.state.openned
});
}
animeMouth = () => {
if(this.mouthInterval){
clearInterval(this.mouthInterval);
}
this.mouthInterval = setInterval(this.handleMouthState, 100);
}
animMouthWithInterval = () => {
if(this.animeInterval){
clearInterval(this.animeInterval);
}
this.animeInterval = setInterval( this.animeMouth, 2000 );
}
componentDidMount() {
setTimeout( this.animMouthWithInterval, 6000);
}