简体   繁体   English

如何在React.js中停止setTimeout onClick

[英]How to stop setTimeout onClick in Reactjs

I am new to Reactjs and while using setTimeOut I got stuck on what is the best way to stop it.Should I use clearTimeOut or stopPropagation() . 我是新来Reactjs ,同时使用setTimeOut我就死在什么是停止it.Should最好的方式,我用clearTimeOutstopPropagation()

This is my code 这是我的代码

render: function() {
    return ( < div className = "colorClick"
      style = {
        {
          background: this.state.bgColor
        }
      }
      onClick = {
        this.detectColor
      } > < /div><br/ >
    );
  },
  calcTime: function() {
    var time = Math.floor((Math.random() * 9) + 1);
    this.setState({
      total_time: time
    }, () => {
      window.setTimeout(this.calcTime, 250)
    });
  },


  detectColor: function(event) {
    window.clearTimeout(this.state.total_time);
    window.clearTimeout(this.calcTime);
  },


  componentDidMount: function() {
    this.calcTime();
  },
  componentWillUnmount: function() {
    this.detectColor();
  }

In this code I am using clearTimeOut but it is not working.I want that when I click on div with className="colorClick" , the setimeOut should clear and reset. 在这段代码中,我使用的是clearTimeOut但它不起作用。我希望当我使用className="colorClick"单击div时,setimeOut应该清除并重置。

You're using clearTimeout incorrectly. 您使用的clearTimeout错误。 You need to store the result of window.setTimeout in a variable, and then call window.clearTimeout with that variable as argument. 您需要将window.setTimeout的结果存储在一个变量中,然后使用该变量作为参数调用window.clearTimeout Here's an example fro w3schools which shows this. 这是w3schools的示例,显示了这一点。

There are some bad practices in your code. 您的代码中有一些不良做法。 I'll comment in the code the changes. 我将在代码中注释更改。 Also, I'm guessing you are using React.createClass , which is deprecated. 另外,我猜您正在使用React.createClass ,它已被弃用。

Use the class syntax instead. 请改用类语法。 and like Horia said, you need to store the timeout in a variable, so the entire code would look something like this: 就像Horia所说的那样,您需要将超时存储在一个变量中,因此整个代码应如下所示:

import React from 'react'
class MyComponent extends React.Component {

  // notice no ":" and no "function"
  render () {
    return (

      <div {/* no space between "<" and the div */}
        className="colorClick" {/* no space around "=" */}
        style={ { background: this.state.bgColor } } {/* no space around "=" */}
        onClick={ this.clearTimeout } {/* no space around "=" */}
      /> {/* div is self closing, use this when there's no childs */}
    );
  } // no ","

  calcTime () {
    // use const if variable is not reassigned
    const time = Math.floor((Math.random() * 9) + 1);
    this.setState({
      total_time: time
    }, () => {
      // assign timeout to component property
      this.timeout = window.setTimeout(this.calcTime, 250)
    });
  }

  // no "event" passed as an argument since its:
  // 1. unnecessary
  // 2. doesn't exit when called in componentWillUnmount
  // method name changed to clearTimeout since it's more accurately describe what it does
  // notice fat arrow syntax to bind clearTimeout context to the class
  clearTimeout = () => {
    // use clearTimeout on the stored timeout in the class property "timeout"
    window.clearTimeout(this.timeout);
  }

  componentDidMount () {
    this.calcTime();
  }

  componentWillUnmount () {
    this.clearTimeout();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM