如何更新此JSONP Feed?

时间:2013-01-29 18:08:58

标签: javascript jquery ajax jsonp

我正在尝试用$&amp ;;显示JSONP数据ajax在网页上的div中。我希望这些数据定期更新。但是下面的代码只显示一次数据,然后它永远不会改变。我该怎么做才能显示数据,然后在Feed中的信息发生变化时实际更改?

<script type = "text/javascript">
   setInterval(function refreshWeather() { 
   $(function() {
   $.ajax({
   url : "http://api.wunderground.com/api/mykey/conditions/forecast/q/autoip.json",
   dataType : "jsonp",
   success : function(parsed_json) { 
   var temp_f = parsed_json['current_observation']['temp_f'];
   var feelslike = parsed_json['current_observation']['feelslike_f'];
   var forecast = parsed_json['forecast']['txt_forecast']['date'];
   $('#content13').replaceWith(temp_f + " feels like " + feelslike);
     }
    });
    });
   }, 90000);

  </script>

1 个答案:

答案 0 :(得分:1)

这似乎有效:

$(function () {
    var refreshWeather = function () {
        $.ajax({
            url: "http://api.wunderground.com/api/mykey/conditions/forecast/q/autoip.json",
            dataType: "jsonp",
            success: function (parsed_json) {
                var temp_f = parsed_json['current_observation']['temp_f'];
                var feelslike = parsed_json['current_observation']['feelslike_f'];
                var forecast = parsed_json['forecast']['txt_forecast']['date'];
                $('#content13').replaceWith(temp_f + " feels like " + feelslike);
            }
        });
    };

    refreshWeather();
    setInterval(refreshWeather, 90000);
});

http://jsfiddle.net/Nsu3h/