2017-02-15 26 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/和適應腳本的東西,你一定不會打破這種限制。即增加超時 –