Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何在JavaScript中执行一段代码一段时间并做出本机反应?_Javascript_Reactjs_React Native - Fatal编程技术网

我如何在JavaScript中执行一段代码一段时间并做出本机反应?

我如何在JavaScript中执行一段代码一段时间并做出本机反应?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我知道它们是存在的,但我从未使用过它们,也不知道哪些是我需要使用的。 我使用的是React Native,我希望代码块只在10秒的时间间隔内执行,时间间隔的下限应该是第一次单击按钮时。这就是我所拥有的: <TouchableHighlight style={styles.button} onPress={() => nClicks++}> <Text style={styles.buttonText}> Tap

我知道它们是存在的,但我从未使用过它们,也不知道哪些是我需要使用的。 我使用的是React Native,我希望代码块只在10秒的时间间隔内执行,时间间隔的下限应该是第一次单击按钮时。这就是我所拥有的:

  <TouchableHighlight style={styles.button}
        onPress={() => nClicks++}>

        <Text style={styles.buttonText}>
         Tap Here!
        </Text>

        </TouchableHighlight>
nClicks++}>
点击这里!
操作的响应非常完美,每单击一次,全局变量的值都会增加1。我只需要找到一种方法来添加时间间隔约束。有什么想法吗?

onPress()
函数中,您可能需要放置
setInterval()
方法。您可以尝试以下代码:

var SetIntervalMixin = {
  componentWillMount: function() {
    this.intervals = [];
  },
  setInterval: function() {
    this.intervals.map(clearInterval);
    this.intervals.push(setInterval.apply(null, arguments));
  }
};

var App = React.createClass({
  mixins: [SetIntervalMixin], // Use the mixin
  getInitialState: function() {
    return {
      seconds: 0,
      nClicks: 0
    };
  },
  onPress: function( plus ) {
    //this.setState({nClicks: plus});
    this.setInterval(this.codeToExcecute, plus * 1000); // Call a method on the mixin
    this.setState({nClicks: this.state.nClicks + 1});
  },
  codeToExcecute: function() {
    console.log('Im the code to excecute');
  },
  render: function() {
    return (
      <div>
        <p>
          React has been running for {this.state.seconds} seconds. nClick: { this.state.nClicks }
        </p>
        <button onClick={ this.onPress.bind(null, this.state.nClicks + 1) }>Click</button>
      </div>
    );
  }
});

React.render(
  <App />,
  document.getElementById('example')
);
var SetIntervalMixin={
componentWillMount:function(){
这个值为[];
},
setInterval:function(){
这个.interval.map(clearInterval);
this.interval.push(setInterval.apply(null,参数));
}
};
var App=React.createClass({
mixin:[SetIntervalMixin],//使用mixin
getInitialState:函数(){
返回{
秒:0,
nClicks:0
};
},
onPress:功能(plus){
//this.setState({nClicks:plus});
this.setInterval(this.codeToExcecute,加上*1000);//在mixin上调用一个方法
this.setState({nClicks:this.state.nClicks+1});
},
codeToExcecute:函数(){
log('Im是要执行的代码');
},
render:function(){
返回(

React已运行了{this.state.seconds}秒。单击:{this.state.nClicks}

点击 ); } }); 反应(

我不清楚你想做什么。