2017-02-15 61 views
0

我在输入字段中输入过快时,我在浏览器中收到禁止错误 403。我使用阵营的js与节点,并在后端快递在输入字段中输入快速时出现禁止错误

以下是我的代码,容纳输入。

import React, {Component, PropTypes} from 'react'; 

export default class SearchBox extends Component { 

    constructor(props) {  
     super(props); 
     this.state = { 
      name: "", 
      typing :false, 
      typingTimeOut :0, 
     }; 
     this.changeName=this.changeName.bind(this); 
     this.sendtoParent=this.sendtoParent.bind(this); 
    } 

    changeName(event) { 
     const self=this; 

     if(self.state.typingTimeOut) 
     { 
      clearTimeout(typingTimeOut); 
     } 

     self.setState({ 
      name: event.target.value, 
      typing:false, 
      typing: setTimeout(function(){ 
       self.sendtoParent(self.state.name)},1000) 
     }); 
    } 

    sendtoParent(){  
     this.props.searching(this.state.name,"true"); 
    } 

    render() { 
     return (
      <div > 
       <input 
        style={styles} 
        id="SearchBox" 
        type="text" 
        placeholder='Enter the name' 
        onChange={this.changeName} 
       />     
      </div> 
     ); 
    } 
} 

我叫去,后来给了我从Github上搜索API所需的JSON父。当我通常类型,但是它给出了快速打字的403错误我的代码是完美的工作。

+1

有什么用'打字:FALSE'?你想在API调用期间阻止打字? –

回答

1

Github上对每秒请求数,您可以发送的数量是有限的。你试图在你的方法引入超时延迟请求的发送,这是一个很好的方法,但它的实现方式,它不会工作。

最简单的方法就是修改changeName功能是这样的:

changeName(event) { 
    const self=this; 

    if(self.typingTimeOut) 
    { 
     clearTimeout(self.typingTimeOut); 
    } 

    self.typingTimeOut = setTimeout(function(){ 
      self.sendtoParent(self.state.name)},1000); 

    self.setState({ 
     name: event.target.value, 
     typing:false 
    }); 
} 
+0

它部分工作。在某些情况下,我仍然遇到错误。要删除错误并重新开始,我需要重新启动我的服务器。有什么方法可以在发生错误之前回滚到状态?因为一旦Forbidden错误正在提交..即使正确的值,或者当我输入缓慢,它仍会继续。 – shinite

+0

看起来你正在接近你的API限制。检查这个网站:https://developer.github.com/v3/rate_limit/和适应脚本的东西,你一定不会打破这种限制。即增加超时 –