【问题标题】:Request timeout请求超时
【发布时间】:2011-08-10 11:37:49
【问题描述】:

我正在使用一些 jQuery 来显示推文,但一旦达到 Twitter API 限制,请求就会发送,但只是不断加载和加载。这看起来不太好。我希望能够确定请求是否花费了太长时间,然后显然会做一些事情,比如取消请求、更改样式等。


所以这是发送请求的代码:

var fileref = document.createElement('script');

fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", "http://search.twitter.com/search.json?q="+buildString+"&callback=TweetTick&rpp=50");

document.getElementsByTagName("head")[0].appendChild(fileref);

这是 TweetTick 函数:

function TweetTick(ob)
{
var container=$('#tweet-container');
container.html('');

$(ob.results).each(function(el){

    /* in here, a div is built for each tweet and then appended to container */

});

container.jScrollPane(); /* just adds the scrollbar */
}

【问题讨论】:

    标签: javascript jquery timeout twitter


    【解决方案1】:

    您需要在服务器端缓存 twitter api 响应。

    How do I keep from running into the rate limit?

    【讨论】:

      【解决方案2】:

      我最近遇到了一个非常相似的问题。我的大部分推特请求都使用 Remy Sharp 的这个脚本:http://remysharp.com/2007/05/18/add-twitter-to-your-blog-step-by-step/

      您需要了解的是,api 超时是针对每个 IP 地址的。因此,如果 api 根据您的测试为您超时,它不会为其他 IP 地址上的其他人超时。现在,如果有人在公司或企业内部访问该网站,而同一地点的其他人也在这样做,那么超时几乎会立即发生。

      要解决此问题,您需要缓存结果。我这样做的方式如下。

      我使用以下代码创建了一个 twitter 缓存系统:

      $twitter_username = "tadywankenobi"; //
      $number_of_tweets = "10";
      
      $options[CURLOPT_URL] = 'http://api.twitter.com/1/statuses/user_timeline.xml?screen_name='.$twitter_username.'&count='.$number_of_tweets.'&include_rts=1';
      $options[CURLOPT_PORT] = 80;
      $options[CURLOPT_FOLLOWLOCATION] = true;
      $options[CURLOPT_RETURNTRANSFER] = true;
      $options[CURLOPT_TIMEOUT] = 60;
      
      $tweets = cache($options);
      
      $twxml = new SimpleXMLElement($tweets);
      
      echo "<ul>";
      for($i=0;$i<=($number_of_tweets-1);$i++){
          $text = $twxml->status[$i]->text;
          echo "<li>".auto_link_twitter($text)."</li>";
      }
      echo "</ul>";
      
      function cache($options) {
      
          $cache_filename = "/var/cache/tweets.xml";
          if(!file_exists($cache_filename)){
              $handle = fopen($cache_filename, 'w') or die('Cannot open file:  '.$my_file);
              fclose($handle);
          }// Check if cache file exists and if not, create it
      
          $time_expire = filectime($cache_filename) + 60*60; // Expire Time (1 hour) // Comment for first run
          // Set time to check file against
      
          if(filectime($cache_filename) >= $time_expire || filesize($cache_filename) == 0) {
              // File is too old or empty, refresh cache
              $curl = curl_init();
              curl_setopt_array($curl, $options);
              $response = curl_exec($curl);
              curl_close($curl);
              if($response){
                  file_put_contents($cache_filename, $response);
              }
              else{
                  unlink($cache_filename);
              }
          }else{
              $response = file_get_contents($cache_filename);
          }
          return $response;
      }
      

      最后缓存函数所做的是在服务器上创建一个文件并将 twitter xml 反馈存储在其中。然后系统检查该文件的年龄,如果它小于一个小时,它会从那里获取结果。否则,它会重新访问 Twitter。您需要在 /var/cache 文件夹中具有可写文件(如果不存在则创建它)。

      我对这段代码做了一些修改,所以如果您遇到任何问题,请告诉我。它还使用 auto_link_twitter() 函数,该函数创建 twitter 文本中所需的链接。这不是我写的,所以我现在会尝试为您找到它的链接。

      希望对大家有所帮助,

      T

      更新:我不记得我在哪里得到了 auto_link_twitter() 函数,所以就在这里。如果写这篇文章的人看到了这篇文章,我很抱歉,我再也找不到出处了。

      function auto_link_twitter($text) {
          // properly formatted URLs
          $urls = "/(((http[s]?:\/\/)|(www\.))?(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";
          $text = preg_replace($urls, " <a href='$1'>$1</a>", $text);
      
          // URLs without protocols
          $text = preg_replace("/href=\"www/", "href=\"http://www", $text);
      
          // Twitter usernames
          $twitter = "/@([A-Za-z0-9_]+)/is";
          $text = preg_replace ($twitter, " <a href='http://twitter.com/$1'>@$1</a>", $text);
      
          // Twitter hashtags
          $hashtag = "/#([A-Aa-z0-9_-]+)/is";
          $text = preg_replace ($hashtag, " <a href='http://twitter.com/#!/search?q=%23$1'>#$1</a>", $text);
          return $text;
      }
      

      【讨论】:

      • 顺便说一句,您可能需要在服务器上设置 SimpleXML,但 PHP 5+ 应该有。
      【解决方案3】:

      您可以使用特定的 jQuery 方法来发出 JSONP 请求。有基本的$.ajax 方法和速记方法$.getJSON 更适合您。要控制请求的超时,您可以使用 timeout 参数。请求超时超时可以使用错误回调处理。

      $.ajax(
          dataType: 'jsonp',
          url: 'http://search.twitter.com/search.json',
          data: {
              q: buildString,
              rpp: 50         
          },
          jsonpCallback: 'TweetTick',
          timeout: 30000,
          error: function(jqXHR, textStatus, errorThrown) {
              if (textStatus == 'timeout') {
                  alert('timeout exceeded');
              }
          }    
      );
      

      【讨论】:

      • 听起来不错,但你能提供一些代码吗?我不确定fileref 的构建和传递有多少需要更改
      猜你喜欢
      • 2011-02-26
      • 1970-01-01
      • 2016-05-27
      • 2020-05-31
      • 2012-03-31
      • 2018-08-17
      • 2011-11-06
      • 2019-02-21
      • 2019-08-09
      相关资源
      最近更新 更多