我正在努力从页面获取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();
最佳答案
您可以在AppInfo
对象上创建一个名为allMatches
的属性,并在数据从jsonp调用返回时设置该属性:
var AppInfo = {
allMatches: null, // NEW PROPERTY TO HOLD RETURNED DATA
confirmDataAvailableOutsideFunction: function () { // NEW FUNCTION TO VERIFY DATA AVAILABLE OUTSIDE getData()
console.dir('AppInfo.allMatches OUTSIDE the function AFTER jsonp call executes: ' + AppInfo.allMatches); //this doesn't come back 'allMatches is not defined'
},
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
AppInfo.allMatches = responseJsonVar["matches"]; // store data in allMatches property
console.dir('allMatches inside the function: ' + AppInfo.allMatches); //this comes back fine
AppInfo.confirmDataAvailableOutsideFunction(); // call test method to verify allMatches property is set
// 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('AppInfo.allMatches OUTSIDE the function BEFORE jsonp call executes: ' + AppInfo.allMatches); //this doesn't come back 'allMatches is not defined'
} //end getdata method
}; //end AppInfo
AppInfo.getData();
请注意,我修改了第二个
console.dir
的文本以指示它在jsonp调用返回之前正在运行,因此此时allMatches
属性仍为null
。这就是为什么在实施@ peter-b建议使用
window.allMatches
而不是局部变量allMatches
之后,window.allMatches
外部函数为undefined
的原因-您在设置之前就对其进行了检查。@ peter-b的解决方案可以很好地工作,前提是您没有尝试在
window.allMatches
设置之前访问它。因此,如果要将数据存储在全局变量中,则可以使用他的方法。如果您希望将其存储在AppInfo对象上,则可以使用我的。另外,您可以将所有内容包装在以
allMatches
作为局部变量的立即函数中:(function () {
var allMatches = null;
var AppInfo = {
getData: function (dataReadyCallback) {
/* ... */
allMatches = responseJsonVar["matches"];
dataReadyCallback();
/* ... */
}
};
AppInfo.getData(allMatchesReady);
function allMatchesReady() {
console.dir('allMatches OUTSIDE the function AFTER jsonp call executes: ' + allMatches);
}
}());
关于javascript - 方法外的可变内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26703270/