无法使用 jquery load() 图像进行变量增量

发布于 2024-12-28 01:18:29 字数 469 浏览 2 评论 0原文

我在 jquery 加载函数中递增变量并将新数据添加到数组时遇到问题。我认为增量和数组操作是在加载所有图像时完成的。但我需要在代码中进一步使用 imgCount 变量。那你能给我解决方案吗?抱歉我的英语不好)))

var imgObject = $('#temp_img img'); // dinamicaly loaded images 

var readyImages = [];
var imgCount = 0;

$(imgObject).bind("load", function() {

    if (this.width > 200 && this.height > 200) {

        readyImages.push(this.src);
        imgCount++;
    }

});

alert(imgCount) // returns 0, though must return 3

i have a problem while incrementing a variable and adding new data to array in the jquery load function. I think that incremention and array manipulation is done when all images are loaded. But I need to use imgCount variable futher in the code. So could you give me the solution? Sorry for my bad english )))

var imgObject = $('#temp_img img'); // dinamicaly loaded images 

var readyImages = [];
var imgCount = 0;

$(imgObject).bind("load", function() {

    if (this.width > 200 && this.height > 200) {

        readyImages.push(this.src);
        imgCount++;
    }

});

alert(imgCount) // returns 0, though must return 3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幻梦 2025-01-04 01:18:29

您定义的回调将异步运行,而警报语句将立即运行。您需要做的是首先计算找到的图像数量,然后在加载所有图像时触发某些功能。类似于:

var imgObject = $('#temp_img img');
var imgCount = imgObject.length - 1;
var imgLoadedCnt = 0;
var ReadyImages = [];
$(imgObject).bind("加载", function() {

if (this.width > 200 && this.height > 200) {
    readyImages.push(this.src);\
    imgLoadedCnt ++;
    if (imgLoadedCnt == imgCount) {
        someFnToRunOnLoaded();
    }
}

});

The callback you define will run asynchronously, while the alert statement will be run immediately. What you would need to do is first count the number of images found, then trigger some functionality when all of them are loaded. Something like:

var imgObject = $('#temp_img img');
var imgCount = imgObject.length - 1;
var imgLoadedCnt = 0;
var readyImages = [];
$(imgObject).bind("load", function() {

if (this.width > 200 && this.height > 200) {
    readyImages.push(this.src);\
    imgLoadedCnt ++;
    if (imgLoadedCnt == imgCount) {
        someFnToRunOnLoaded();
    }
}

});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文