我不确定为什么我的功能没有返回数据。
loadingImages = function (id)
{
var totalImages = $(id+" img").length;
var imagesLoaded = 0;
var result = null;
$(id).find('img').each(function(elem) {
var fakeSrc = $(this).attr('src');
$(this).attr("src", fakeSrc).load(function() {
imagesLoaded++;
if (imagesLoaded >= totalImages) {
result = true;
} else {
result = false;
}
console.log(result);
});
});
return result;
}
var $id = "#elem";
var rr = loadingImages($id);
console.log(rr);
任何帮助都会受到赞赏,这只是个人学习代码,所以我不急。
答案 0 :(得分:0)
null
不会立即触发,因此您的结果将始终为loadingImages = function (id, cb) {
var totalImages = $(id+" img").length;
var imagesLoaded = 0;
var result = null;
$(id).find('img').each(function(elem) {
var fakeSrc = $(this).attr('src');
$(this).attr("src", fakeSrc).load(function() {
imagesLoaded++;
if (imagesLoaded >= totalImages) {
result = true;
cb(result);
}
});
});
}
var $id = "#elem";
loadingImages($id, function(rr) {
console.log(rr);
});
。您应该更改代码,以便函数接受回调:
## [,1] [,2] [,3] [,4]
## [1,] 2 0 0 0
## [2,] 1 1 0 1
## [3,] 0 1 0 0
## [4,] 1 1 1 3
, where 0 denotes inaccessible points, 1 denotes accessible points.
2 denotes the starting point, and 3 denotes the destination.