【问题标题】:Fetching Twitter Feed with JSON and JQuery - Automatically Refresh?使用 JSON 和 JQuery 获取 Twitter 提要 - 自动刷新?
【发布时间】:2012-01-31 19:45:05
【问题描述】:

我正在编写一个脚本,该脚本将从 Twitter API 中“获取”最新的推文,然后使用 JQuery 在我的页面上将它们显示为 HTML。

我是 JQuery 的新手,所以如果有人能指出 JQuery 网站上必要功能的方向,我将不胜感激。

我目前已经构建了以下脚本:

<!-- Use the Google jQuery CDN for lib support -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- Setup and fetch the JSON data -->
<script type="text/javascript">
$(document).ready(function(){
var url='http://search.twitter.com/search.json?callback=?&q=@req';
$.getJSON(url,function(json){
<!-- Iterate the file -->
        $.each(json.results,function(i,tweet){
           $("#results").append('<p><img src="'+tweet.profile_image_url+'" widt="48" height="48" />'+tweet.text+'</p>');
$("#results").slideDown("slow");
        });
                    });
});
</script>
<!-- Output the file into the DIV -->
<div id="results"></div>

脚本运行良好,但我现在想加入某种形式的内容自动刷新。即每 x 分钟“重新获取”一次提要。

据我了解,我需要将 .append 替换为 .html 以便在重新加载之前从页面中删除内容,但是是否有人对实际刷新内容的最佳方式有任何建议?我发现可能有文章表达了对浏览器内存泄漏等问题的担忧,并且不想走错路。

期待您的回复,再次感谢。

【问题讨论】:

    标签: jquery json twitter refresh


    【解决方案1】:

    为什么人们总是想在最基本的事情上使用像 jQuery 这样的庞大库?

    Javascript 的 setTimeoutsetInterval 可以为您做到这一点。

    【讨论】:

      【解决方案2】:
      $(function(){
          function getTweets() {
              var url='http://search.twitter.com/search.json?callback=?&q=@req';
              $.getJSON(url,function(json){
      
                  //setup an array to buffer output
                  var output = [];
      
                  //a for loop will perform faster when setup like this
                  for (var i = 0, len = json.results.length; i < len; i++) {
      
                     //instead of appending each result, add each to the buffer array
                     output.push('<p><img src="' + json.results[i].profile_image_url + '" widt="48" height="48" />' + json.results[i].text + '</p>');
                  }
      
                  //now select the #results element only once and append all the output at once, then slide it into view
                  $("#results").html(output.join('')).slideDown('slow');
              });
          }
      
          //set an interval to run the getTweets function (30,000 ms is 5 minutes), you can cancel the interval by calling clearInterval(timer);
          var timer = setInterval(getTweets, 30000);
      
          //run the getTweets function on document.ready
          getTweets();
      
      });
      

      这里有一些关于window.setInterval() 的好文档:https://developer.mozilla.org/en/window.setInterval

      【讨论】:

      • 为了确保你不会堆叠调用,以防服务器响应缓慢,我想最好使用setTimeout,然后从getTweets内部设置一个新的超时来调用自己在 X 秒内再次 - 在 AJAX 调用完成后。 50 秒我猜你也可以使用 setInterval,但如果他想每 10 秒运行一次,AJAX 调用就有排队的风险。
      • 感谢 Jasper 和 @Christofer Eliasson 的及时回复。我已经实现了你的 Jasper 代码,这正是我想要做的关于刷新的事情,所以谢谢你。正如我在最初的帖子中提到的,我现在希望确保在重新加载时删除内容。我已经用 .html 替换了 .append ,这似乎可以解决问题,但是请您确认这实际上是在重新加载之前删除了内容,而不是完全做其他事情。
      • Christofer,我希望大约每 60 秒刷新一次内容,但是我应该选择减少时间限制的最坏情况是什么?
      • @user1055774 .html(&lt;CODE&gt;).empty().append(&lt;CODE&gt;) 是一回事。
      • @user1055774 当您使用 setInterval 时,该函数将再次运行,无论之前对该函数的调用是否仍在执行 - 以防 Twitter API 变慢,而您没有得到在您的分钟间隔内响应,您将开始堆叠 AJAX 请求。我说要么使用 setTimeout 代替,如我之前的评论中所述,要么将 .getJSON() 更改为使用 .ajax() 方法,并指定 timeout-option 比您的时间间隔更短。无论哪种方式,您都可以避免获得 AJAX 请求队列。
      猜你喜欢
      • 2012-06-24
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 2015-11-18
      相关资源
      最近更新 更多