2013-02-20 37 views
0

我想用雅虎的YQL调用多个函数。我试图展示的是每一天的天气。 XML文件在今天[1]明天[2] [0]后有[0]等。当我运行代码时,它会查看最后一个被调用的数字,而不会加载其他数字。我哪里错了?在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(weatherone){ 

var query = "select * from rss where url='http://xml.weather.yahoo.com/forecastrss/SPXX0050_f.xml'"; 
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.item.forecast[0]; 
    $('#wxDay').text(info.day); 
    $('#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="74" height="74" title="' + info.text + '" />'); 
    $('#wxTemp').html(info.high + '&deg;' + (u.toUpperCase())); 
    $('#wxText').html(info.text); 
}; 

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


}); 

$(function(weathertwo){ 

var query = "select * from rss where url='http://xml.weather.yahoo.com/forecastrss/SPXX0239_f.xml'"; 
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.item.forecast[1]; 
    $('#wxDay').text(info.day); 
    $('#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="74" height="74" title="' + info.text + '" />'); 
    $('#wxTemp').html(info.high + '&deg;' + (u.toUpperCase())); 
    $('#wxText').html(info.text); 
}; 

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


}); 
</script> 
+0

WTH您是否使用自定义'jsonpCallback'? – Bergi 2013-02-20 22:39:35

回答

0

你做

window['wxCallback'] = function(data) { ... }; 
$.ajax({ ..., jsonpCallback: 'wxCallback' }); 

两次,第二则会覆盖第一。使用不同的回叫名称,所以

window['wxCallback1'] = ...; 
$.ajax({ ..., jsonpCallback: 'wxCallback1' }); 

window['wxCallback2'] = ...; 
$.ajax({ ..., jsonpCallback: 'wxCallback2' }); 
+0

感谢您向我解释这一点。完美的作品! – ServerSideSkittles 2013-02-20 23:02:43

相关问题