我正在努力從頁面獲取jsonp信息,並且希望在該信息上運行各種函數。信息恢復正常,但我似乎無法找到一種方法使其在功能之外訪問。我知道這是關閉和功能範圍,但我不知道如何使它的工作,任何想法?方法之外的變量內容
我可以通過對json文件進行多次調用來實現我想要在腳本的其餘部分做的事情,但我認爲最好只查詢json一次並將其彈出到變量中,然後嘗試關閉?我對這個設置比較陌生,所以有任何建議。
有效地從下面的代碼中,我希望能夠在getData方法運行之後獲得可訪問的allMatches變量。
感謝您的時間,所有幫助非常感謝。
var AppInfo = {
getData : function(){
var responseJsonVar;
var callbackName, script, newInfo, mydata,allMatches;
// Get a random name for the callback
callbackName = "checkGames" + new Date().getTime() + Math.floor(Math.random() * 10000);
// create the jsonP script call on the page
script = document.createElement('script');
script.src = "http://www.hookhockey.com/index.php/temp-gillian/?callback=" + callbackName;
document.documentElement.appendChild(script);
// call the json
window[callbackName] = function(data) {
responseJsonVar = data; // this is the info back from the json file
//the filtered data source from json
var allMatches = responseJsonVar["matches"];
console.dir('allMatches inside the function: ' + allMatches); //this comes back fine
// Remove our callback ('delete' with 'window properties fails on some versions of IE, so we fall back to setting the property to 'undefined' if that happens)
try {
delete window[callbackName];
}
catch (e) {
window[callbackName] = undefined;
}
//I've tried putting a return value (return allMatches) in here and then calling window[callbackName]() outside of the function but I get undefined for matches
}; // end window[callbackName] function
//this is what I think I should be doing to get the info out on its own
console.dir('allMatches OUTSIDE the function: ' + allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
} //end AppInfo
AppInfo.getData();
太好了,非常感謝您的信息,非常感謝! – 2014-11-04 20:43:19
不客氣!很高興有幫助。 – MikeJ 2014-11-04 23:21:10