仅刷新JavaScript部分?

时间:2013-02-15 00:37:18

标签: javascript

我有一段JavaScript显示当前温度,但它不会随温度变化而更新。有没有办法在计时器上更新JavaScript部分?

编辑:对不起,这是代码(不是我创建的)。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
// javascript will go here
$(function(){

// Specify the ZIP/location code and units (f or c)
var loc = '10001'; // or e.g. SPXX0050
var u = 'f';

var query = "SELECT item.condition FROM weather.forecast WHERE location='" + loc + "' AND u='" + u + "'";
var cacheBuster = Math.floor((new Date().getTime()) / 1200 / 1000);
var url = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent(query) + '&format=json&_nocache=' + cacheBuster;

window['wxCallback'] = function(data) {
    var info = data.query.results.channel.item.condition;
    $('#wxIcon').css({
        backgroundPosition: '-' + (61 * info.code) + 'px 0'
    }).attr({
        title: info.text
    });
    $('#wxIcon2').append('<img src="http://l.yimg.com/a/i/us/we/52/' + info.code + '.gif" width="40" height="40" title="' + info.text + '" />');
    $('#wxTemp').html(info.temp + '&deg;' + (u.toUpperCase()));
};

$.ajax({
    url: url,
    dataType: 'jsonp',
    cache: true,
    jsonpCallback: 'wxCallback'
});
});
</script>

1 个答案:

答案 0 :(得分:1)

为您的函数命名(而不是使用$(function(){...),例如GetWeather

然后在外面......

setInterval(GetWeather,10000)

尝试:http://codepen.io/anon/pen/bvyDh