使用 $.each 进行迭代时
在迭代包含数据的对象时,我正在检查图像的 url 对于函数是否有效。问题是函数检查图像仅针对最后一个对象运行 8 次。任何人都可以看到为什么?
功能检查图像:
function checkImage(url, succ, err){
var checkImg = new Image();
checkImg.src = url;
checkImg.onerror = function() {
err();
};
}
和.each部分:
var obj = {},
searchResults = [];
$.each(data, function(i, m){
obj = {'image': m.park_photo1_290x220, 'name': m.park_name, 'developer': m.park_developer_name,
'location': m.park_location_name, 'exact_location': m.park_location_parent_name,
'total_area': m.area, 'free_area': m.free_area, 'id': m.id, 'lat': m.park_lat, 'lon': m.park_lon};
var uri = obj.image;
console.log('object');
checkImage(uri, function(){
obj.image = uri;
}, function() {
console.log(x++);
console.log(obj.name+','+obj.image);
obj.image = 'images/no_foto_ssmall.png';
console.log(obj.image);
});
searchObjects.push(obj);
$('#search-results').append(''+
'<li class=\"ui-li-has-thumb ui-btn ui-btn-icon-right ui-li ui-btn-up-c\" role=\"option\" tabindex=\"'+ i +'\" data-theme=\"c\"><div>'+
'<img src=\"'+ searchObjects[i].image +'\" class="ui-li-thumb">'+
'<h3 class="ui-li-heading"><a href="#" class="ui-link-inherit single" name=\"'+ i +'\">'+
searchObjects[i].name +'</a></h3></div>'+
'<div class=\"search-detail\"><p>'+ searchObjects[i].developer +'</p></div>'+
'<div class=\"search-detail\"><p>'+ searchObjects[i].location +'</p></div>'+
'<span class=\"ui-icon ui-icon-arrow-r\"></span></li>'
);
});
firebug回溯:
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
error1
1
Panattoni Park 1,/static/realestate/wp/wpp-88645820868768.jpg.290x220_q85.jpg
images/no_foto_ssmall.png
error1
2
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
3
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
4
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
5
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
6
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
7
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
8
Panattoni Park 1,images/n
o_foto_ssmall.png images/no_foto_ssmall.png
按要求编辑
,我的对象:
var data = [{"total_area": 10800.0, "developer_id": 1511,
"photo1_290x220": "/static/realestate/wp/wpp176885389187907.jpg.290x220_q85.jpg",
"location_parent_name": "BZZ", "agent_phone_fax": "",
"location_id": 147, "id": 760, "developer_fax": "", "location_name": "ABC",
"lon": 10.870075199999999, "object_id": 201, "developer_name": "Orpo",
"agent_id": 8,"lat": 52.2079223, "warehouses_counter": 1,
"name": "Park 1","agent_phone_mobile": "+48 668 124 152", "featured": false,
"location_parent_id": 106},
{"total_area": 30780.0, "developer_id": 193,
"photo1_290x220": "/static/realestate/wp/wpp195908774409312.JPG.290x220_q85.jpg",
"location_parent_name": "CDE", "agent_phone_fax": "", "location_id": 138,
"id": 761, "developer_fax": "", "location_name": "XYZ",
"lon": 20.9232473, "object_id": 187, "developer_name": "Orco","lat": 32.171918300000002,
"warehouses_counter": 5, "name": "Park 2","featured": false,
"location_parent_id": 106}]
While iterating through an object containing data I'm checking if the url of image is valid with a function. Problem is that function checking image is run only for the last object, 8 times. Anyone can see why ?
function checking image :
function checkImage(url, succ, err){
var checkImg = new Image();
checkImg.src = url;
checkImg.onerror = function() {
err();
};
}
and the .each part:
var obj = {},
searchResults = [];
$.each(data, function(i, m){
obj = {'image': m.park_photo1_290x220, 'name': m.park_name, 'developer': m.park_developer_name,
'location': m.park_location_name, 'exact_location': m.park_location_parent_name,
'total_area': m.area, 'free_area': m.free_area, 'id': m.id, 'lat': m.park_lat, 'lon': m.park_lon};
var uri = obj.image;
console.log('object');
checkImage(uri, function(){
obj.image = uri;
}, function() {
console.log(x++);
console.log(obj.name+','+obj.image);
obj.image = 'images/no_foto_ssmall.png';
console.log(obj.image);
});
searchObjects.push(obj);
$('#search-results').append(''+
'<li class=\"ui-li-has-thumb ui-btn ui-btn-icon-right ui-li ui-btn-up-c\" role=\"option\" tabindex=\"'+ i +'\" data-theme=\"c\"><div>'+
'<img src=\"'+ searchObjects[i].image +'\" class="ui-li-thumb">'+
'<h3 class="ui-li-heading"><a href="#" class="ui-link-inherit single" name=\"'+ i +'\">'+
searchObjects[i].name +'</a></h3></div>'+
'<div class=\"search-detail\"><p>'+ searchObjects[i].developer +'</p></div>'+
'<div class=\"search-detail\"><p>'+ searchObjects[i].location +'</p></div>'+
'<span class=\"ui-icon ui-icon-arrow-r\"></span></li>'
);
});
firebug traceback :
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
object
error1
1
Panattoni Park 1,/static/realestate/wp/wpp-88645820868768.jpg.290x220_q85.jpg
images/no_foto_ssmall.png
error1
2
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
3
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
4
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
5
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
6
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
7
Panattoni Park 1,images/no_foto_ssmall.png
images/no_foto_ssmall.png
error1
8
Panattoni Park 1,images/n
o_foto_ssmall.png
images/no_foto_ssmall.png
EDIT
as requested, my object :
var data = [{"total_area": 10800.0, "developer_id": 1511,
"photo1_290x220": "/static/realestate/wp/wpp176885389187907.jpg.290x220_q85.jpg",
"location_parent_name": "BZZ", "agent_phone_fax": "",
"location_id": 147, "id": 760, "developer_fax": "", "location_name": "ABC",
"lon": 10.870075199999999, "object_id": 201, "developer_name": "Orpo",
"agent_id": 8,"lat": 52.2079223, "warehouses_counter": 1,
"name": "Park 1","agent_phone_mobile": "+48 668 124 152", "featured": false,
"location_parent_id": 106},
{"total_area": 30780.0, "developer_id": 193,
"photo1_290x220": "/static/realestate/wp/wpp195908774409312.JPG.290x220_q85.jpg",
"location_parent_name": "CDE", "agent_phone_fax": "", "location_id": 138,
"id": 761, "developer_fax": "", "location_name": "XYZ",
"lon": 20.9232473, "object_id": 187, "developer_name": "Orco","lat": 32.171918300000002,
"warehouses_counter": 5, "name": "Park 2","featured": false,
"location_parent_id": 106}]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所有闭包都在同一范围内关闭,因此同一个 obj 实例,因为在调用 obj 时异步调用 onerror ,已经是最后一个值了。
解决方案应该是在
obj
之前添加一个var
:这个 小 jsfiddle demo 执行两次相同的代码,一次有修复,一次没有修复。
All of your closures closes on the same scope and so the same
obj
instance, asonerror
is called asynchronously at the time it is calledobj
is already the last value.The solution should be to add a
var
beforeobj
:This small jsfiddle demo execute twice the same code, once with and once without the fix.
看起来您的变量
obj
定义超出了范围,因此无法正确评估它。可能
searchResults
变量应该是searchObjects
之一。看起来您试图从哈希中读取不正确的数据,因此它几乎在所有地方都会返回
undefined
。我的这段代码可以帮助你:
这在fiddler http://jsfiddle.net/nC7WA/1/ 上聚集在一起
It looks like you have variable
obj
defined out of scope, so it could not be evaluated correctly.Possibly
searchResults
variable should besearchObjects
one.And it looks like you trying to read incorrect data from your hash, so it returns
undefined
almost everywhere.My be this code can help you:
And this gathered together on fiddler http://jsfiddle.net/nC7WA/1/