为什么 return 值不是最大值?
Why the return value is not the max value?
我的问题是,当我注释掉 "return maxID;" 时,我得到了我拥有的 ID 数组的结果(例如 1 2 3 4 5),但是如果我尝试 return maxID,结果是 1 而不是 5。为什么??如何解决?
这是我的代码:
function storeData() {
var maxID;
var query = "SELECT 'ROWID', ID FROM " +
'mytableID';
var encodedQuery = encodeURIComponent(query);
// Construct the URL
var url = ['https://www.googleapis.com/fusiontables/v1/query'];
url.push('?sql=' + encodedQuery);
url.push('&key=myclientKey');
url.push('&callback=?');
// Send the JSONP request using jQuery
$.ajax({
url: url.join(''),
dataType: 'jsonp',
success: function (data) {
var rows = data['rows'];
var ftData = document.getElementById('ft-data');
maxID = 0;
for (var i in rows) {
var rec = rows[i][0];
var collection = rows [i][1];
var idValue = parseInt (collection);
if (idValue > maxID)
maxID = idValue;
var dataElement = document.createElement('div');
var recElement = document.createElement('p');
recElement.innerHTML = rec;
var collectionElement = document.createElement('p');
collectionElement.innerHTML = collection;
document.write(maxID);
return maxID;
});
}
return
来得太早了,它在循环里面,你在第一行之后留下success
。
基本上 return
在 ajax 回调中没有用,只需在 success
.
的末尾打印 maxID
已修复 $.ajax
-呼叫:
// Send the JSONP request using jQuery
$.ajax({
url: url.join(''),
dataType: 'jsonp',
success: function(data) {
var rows = data['rows'];
maxID = 0;
//don't use for-in-loops to iterate over arrays
//for (var i in rows) {
for (var i = 0; i < rows.length; ++i) {
var rec = rows[i][0];
var collection = rows[i][1];
var idValue = parseInt(collection);
if (idValue > maxID){maxID = idValue;}
var dataElement = document.createElement('div');
var recElement = document.createElement('p');
recElement.innerHTML = rec;
var collectionElement = document.createElement('p');
collectionElement.innerHTML = collection;
}
//you shouldn't use write after the document has been loaded
//document.write(maxID);
document.body.innerHTML = maxID;
}
});
我的问题是,当我注释掉 "return maxID;" 时,我得到了我拥有的 ID 数组的结果(例如 1 2 3 4 5),但是如果我尝试 return maxID,结果是 1 而不是 5。为什么??如何解决? 这是我的代码:
function storeData() {
var maxID;
var query = "SELECT 'ROWID', ID FROM " +
'mytableID';
var encodedQuery = encodeURIComponent(query);
// Construct the URL
var url = ['https://www.googleapis.com/fusiontables/v1/query'];
url.push('?sql=' + encodedQuery);
url.push('&key=myclientKey');
url.push('&callback=?');
// Send the JSONP request using jQuery
$.ajax({
url: url.join(''),
dataType: 'jsonp',
success: function (data) {
var rows = data['rows'];
var ftData = document.getElementById('ft-data');
maxID = 0;
for (var i in rows) {
var rec = rows[i][0];
var collection = rows [i][1];
var idValue = parseInt (collection);
if (idValue > maxID)
maxID = idValue;
var dataElement = document.createElement('div');
var recElement = document.createElement('p');
recElement.innerHTML = rec;
var collectionElement = document.createElement('p');
collectionElement.innerHTML = collection;
document.write(maxID);
return maxID;
});
}
return
来得太早了,它在循环里面,你在第一行之后留下success
。
基本上 return
在 ajax 回调中没有用,只需在 success
.
maxID
已修复 $.ajax
-呼叫:
// Send the JSONP request using jQuery $.ajax({ url: url.join(''), dataType: 'jsonp', success: function(data) { var rows = data['rows']; maxID = 0; //don't use for-in-loops to iterate over arrays //for (var i in rows) {for (var i = 0; i < rows.length; ++i) { var rec = rows[i][0]; var collection = rows[i][1]; var idValue = parseInt(collection); if (idValue > maxID){maxID = idValue;} var dataElement = document.createElement('div'); var recElement = document.createElement('p'); recElement.innerHTML = rec; var collectionElement = document.createElement('p'); collectionElement.innerHTML = collection; } //you shouldn't use write after the document has been loaded //document.write(maxID);document.body.innerHTML = maxID; } });