传递变量jQuery的AJAX的成功回调函数回调、变量、函数、AJAX

2023-09-10 17:13:59 作者:只愿君心似我心

我想preLOAD用jQuery的AJAX调用一些图像,但我有一个传递(URL)字符串到了AJAX调用成功函数中的一个函数实际问题(如果是有道理的)。

I am trying to preload some images with a jQuery AJAX call, but am having real problems passing a (url) string into a function within the success function of the AJAX call (if that makes sense).

下面是我的code因为是看台:

Here is my code as is stands:

    //preloader for images on gallery pages
    window.onload = function() {
        setTimeout(function() {     
            var urls = ["./img/party/"]; //just one to get started

            for ( var i = 0; i < urls.length; i++ ) {
                $.ajax({
                    url: urls[i],
                    success: function(data,url){
                        $(data).find("a:contains(.jpg)").each(function(url){                                
                            new Image().src = url + $(this).attr("href");
                        });
                    }
                });
            };  
        }, 1000);
    };

可以看到我的(失败的)尝试通过传递URL到每个()通话 - 网​​址最终需要增加的整数的值。不知道为什么,或这些涉及什么,对JPG文件也许是多少?

One can see my (failed) attempt at passing the url through into the .each() call - url ends up takes the value of increasing integers. Not sure why or what these relate to, maybe the number of jpg files?

...无论如何,它当然应该取一个值在我原来的网址阵列。

...anyway, it should of course take the single value in my original urls array.

感谢您的帮助 - 我似乎总是得到一个位与这些回调扭曲的

Thanks for any help - I always seem to get in a bit of a twist with these callbacks.

陆侃?

所以,我搞乱了一下周围,以从@ron tornambe和@PiSquared意见听取和我目前的位置:

So, I mucked around a bit, taking heed of comments from @ron tornambe and @PiSquared and am currently here:

    //preloader for images on gallery pages
    window.onload = function() {
        var urls = ["./img/party/","./img/wedding/","./img/wedding/tree/"];

        setTimeout(function() {
            for ( var i = 0; i < urls.length; i++ ) {
                $.ajax({
                    url: urls[i],
                    success: function(data) {
                        image_link(data,i);

                        function image_link(data, i) {
                            $(data).find("a:contains(.jpg)").each(function(){ 
                                console.log(i);
                                new Image().src = urls[i] + $(this).attr("href");
                            });
                        }
                    }
                });
            };  
        }, 1000);       
    };

我试图把图片链接(资料,我)在这里四处奔走(每个嵌套函数等),但我得到了相同的结果:对于价值只会被记录为3。我怀疑这是因为所有的引用指向同样的事情,由时间异步任务实际到达图片链接(资料,我)的... 循环圆满结束(因此具有3的值)。不用说,这给了网​​址[I] 为未定义。

I tried putting the image_link(data, i) here there and everywhere (in each nested function etc.) but I get the same result: the value for i is only ever logged as 3. I suspect that this is because all references to i point to the same thing and by the time the asynchronous task actually gets to image_link(data, i) the for... loop is over and done with (and hence has a value of 3). Needless to say this gives urls[i] as 'undefined'.

任何(更多)的提示我怎么能避开这个问题?

Any (more) tips how I can get round this?

推荐答案

我只是遇到了类似的问题,并相信我已经找到了解决方案。

I was just experiencing a similar issue, and believe I have found a solution.

由于设置对象绑定到了AJAX调用,只需在索引器添加为自定义设置,然后你就可以访问使用的成功回调

Since the settings object is tied to that ajax call, you can simply add in the indexer as a custom setting, which you can then access using this in the success callback:

//preloader for images on gallery pages
window.onload = function() {
    var urls = ["./img/party/","./img/wedding/","./img/wedding/tree/"];

    setTimeout(function() {
        for ( var i = 0; i < urls.length; i++ ) {
            $.ajax({
                url: urls[i],
                indexValue: i,
                success: function(data) {
                    image_link(data , this.indexValue);

                    function image_link(data, i) {
                        $(data).find("a:contains(.jpg)").each(function(){ 
                            console.log(i);
                            new Image().src = urls[i] + $(this).attr("href");
                        });
                    }
                }
            });
        };  
    }, 1000);       
};

希望这是卓有成效的。

Hopefully that does the trick.