实现短时间内连续点击多次触发事件

借助闭包实现短时间内连续点击多次触发事件

startBluetooth = () => {
    // 在2s内连点5次进入页面
    let clickCount = 0; // 点击次数
    let timeFlag = true; // 是否添加计时
    return () => {
      clickCount += 1;
      if (clickCount >= 5) {
        this.setState({  // 这是写筒子们自己的逻辑
          isOpened: true
        })
      }
      if (timeFlag) { // 多次点击只触发一次
        timeFlag = false;
        setTimeout(() => {
          clickCount = 0;
          timeFlag = true;
        }, 2000);
      }
    }
  }

页面调用(这里要注意,不能写成onClick={() => {this.startBluetooth()()} )

<View className='user-info-white-block-title' onClick={this.startBluetooth()} >
 点击按钮
</View>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容