Javascript 不定期更新
我真的不知道为什么会发生这个问题。 假设它每秒更新90度。 有时会变得疯狂,有时会更新90度以上 选项卡未聚焦时,“设置间隔”和“设置超时”不同步 您必须更改异步点,如下所示:Javascript 不定期更新,javascript,reactjs,setstate,Javascript,Reactjs,Setstate,我真的不知道为什么会发生这个问题。 假设它每秒更新90度。 有时会变得疯狂,有时会更新90度以上 选项卡未聚焦时,“设置间隔”和“设置超时”不同步 您必须更改异步点,如下所示: async doThis () { const updateValue = _=>{ if (!document.hidden){ console.log(this.state.value + 90); th
async doThis () {
const updateValue = _=>{
if (!document.hidden){
console.log(this.state.value + 90);
this.setState(s=>({value: s.value + 90}))
}
}
this.timerId = setInterval(updateValue, 1000)
}
我这么做了,我想你的问题解决了。。。请使用此代码:
const {useState} = React;
class App extends React.Component{
constructor(){
super();
this.state = {
value: 0
}
this.doThis = this.doThis.bind(this);
}
componentDidMount() {
this.doThis();
}
componentWillUnmount() {
clearInterval(this.timerId);
}
async doThis () {
const updateValue = _=>{
if (!document.hidden){
console.log(this.state.value + 90);
this.setState(s=>({value: s.value + 90}))
}
}
this.timerId = setInterval(updateValue, 1000)
}
render() {
return <Box value={this.state.value}/>;
}
}
function Box(props) {
const angle = props.value;
let style = {
transform: `rotateZ(${angle}deg)`,
transformOrigin: 'bottom'
}
let boxStyle = {
display: 'flex',
justifyContent: 'center'
}
return(
<div>
<div className='box' style={boxStyle}>
<div style={style} className="needle"></div>
</div>
</div>
)
}
ReactDOM.render(<App/>, document.getElementById('root'));
const{useState}=React;
类应用程序扩展了React.Component{
构造函数(){
超级();
此.state={
数值:0
}
this.doThis=this.doThis.bind(this);
}
componentDidMount(){
这是什么;
}
组件将卸载(){
clearInterval(this.timerId);
}
异步doThis(){
const updateValue=\u=>{
如果(!document.hidden){
console.log(this.state.value+90);
this.setState(s=>({value:s.value+90}))
}
}
this.timerId=setInterval(updateValue,1000)
}
render(){
返回;
}
}
功能盒(道具){
常量角度=道具值;
让样式={
变换:`rotateZ(${angle}deg)`,
变换原点:“底部”
}
让boxStyle={
显示:“flex”,
为内容辩护:“中心”
}
返回(
)
}
ReactDOM.render(,document.getElementById('root'));
选项卡未聚焦时,“设置间隔”和“设置超时”不同步
您必须更改异步点,如下所示:
async doThis () {
const updateValue = _=>{
if (!document.hidden){
console.log(this.state.value + 90);
this.setState(s=>({value: s.value + 90}))
}
}
this.timerId = setInterval(updateValue, 1000)
}
我这么做了,我想你的问题解决了。。。请使用此代码:
const {useState} = React;
class App extends React.Component{
constructor(){
super();
this.state = {
value: 0
}
this.doThis = this.doThis.bind(this);
}
componentDidMount() {
this.doThis();
}
componentWillUnmount() {
clearInterval(this.timerId);
}
async doThis () {
const updateValue = _=>{
if (!document.hidden){
console.log(this.state.value + 90);
this.setState(s=>({value: s.value + 90}))
}
}
this.timerId = setInterval(updateValue, 1000)
}
render() {
return <Box value={this.state.value}/>;
}
}
function Box(props) {
const angle = props.value;
let style = {
transform: `rotateZ(${angle}deg)`,
transformOrigin: 'bottom'
}
let boxStyle = {
display: 'flex',
justifyContent: 'center'
}
return(
<div>
<div className='box' style={boxStyle}>
<div style={style} className="needle"></div>
</div>
</div>
)
}
ReactDOM.render(<App/>, document.getElementById('root'));
const{useState}=React;
类应用程序扩展了React.Component{
构造函数(){
超级();
此.state={
数值:0
}
this.doThis=this.doThis.bind(this);
}
componentDidMount(){
这是什么;
}
组件将卸载(){
clearInterval(this.timerId);
}
异步doThis(){
const updateValue=\u=>{
如果(!document.hidden){
console.log(this.state.value+90);
this.setState(s=>({value:s.value+90}))
}
}
this.timerId=setInterval(updateValue,1000)
}
render(){
返回;
}
}
功能盒(道具){
常量角度=道具值;
让样式={
变换:`rotateZ(${angle}deg)`,
变换原点:“底部”
}
让boxStyle={
显示:“flex”,
为内容辩护:“中心”
}
返回(
)
}
ReactDOM.render(,document.getElementById('root'));
抱歉,它似乎减缓了不规则的速度,但没有阻止它。抱歉,它似乎减缓了不规则的速度,但没有阻止它。