Pass variable to function in jquery AJAX success callback

Jon

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).

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);
};

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.


PROGESS?

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);       
};

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?

Paul Zaczkowski

Since the settings object is tied to that ajax call, you can simply add in the indexer as a custom property, 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);       
};

Edit: Adding in an updated JSFiddle example, as they seem to have changed how their ECHO endpoints work: https://jsfiddle.net/djujx97n/26/.

To understand how this works see the "context" field on the ajaxSettings object: http://api.jquery.com/jquery.ajax/, specifically this note:

"The this reference within all callbacks is the object in the context option passed to $.ajax in the settings; if context is not specified, this is a reference to the Ajax settings themselves."

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

AJAX success callback only carrying out first function

From Dev

Jquery Pjax - Ajax success function

From Dev

jQuery: using $(this) inside of $.ajax success function

From Dev

Including Success / Fail to jQuery / Ajax function

From Dev

How can I get in 'clicked' element in jquery ajax success callback

From Dev

How to pass argument to success callback in casperjs waitForSelector function?

From Dev

Jquery ajax function how to pass variable

From Dev

Is there a way to pass variables from beforeSend to success functions in the jQuery .ajax function?

From Dev

exec function before every ajax success callback

From Dev

jQuery AJAX return the function as data upon success

From Dev

jQuery/Ajax success function not rendering html

From Dev

Ajax: How to pass variable one function to another jQuery

From Dev

Repeating jQuery Ajax success function

From Dev

jQuery Ajax success callback not firing nor returning error

From Dev

how to read javascript variable from ajax success callback function

From Dev

How to pass $(this) in success callback of $.ajax

From Dev

Pass a socket io connection to an ajax success function

From Dev

how to pass a variable to a Page Method Success callback function

From Dev

JQuery Ajax Type error in success callback

From Dev

AJAX success callback only carrying out first function

From Dev

Jquery Ajax Callback External Function

From Dev

JQuery ajax success fired twice, because of .done callback

From Dev

jquery ajax success not finding variable div

From Dev

jQuery Custom Event not triggering in AJAX Success Callback

From Dev

Ajax: How to pass variable one function to another jQuery

From Dev

how to read javascript variable from ajax success callback function

From Dev

jQuery - Use variable in AJAX request for it's success callback

From Dev

jquery ajax success redirect as a variable url

From Dev

Nested AJAX requests without a callback success function

Related Related

  1. 1

    AJAX success callback only carrying out first function

  2. 2

    Jquery Pjax - Ajax success function

  3. 3

    jQuery: using $(this) inside of $.ajax success function

  4. 4

    Including Success / Fail to jQuery / Ajax function

  5. 5

    How can I get in 'clicked' element in jquery ajax success callback

  6. 6

    How to pass argument to success callback in casperjs waitForSelector function?

  7. 7

    Jquery ajax function how to pass variable

  8. 8

    Is there a way to pass variables from beforeSend to success functions in the jQuery .ajax function?

  9. 9

    exec function before every ajax success callback

  10. 10

    jQuery AJAX return the function as data upon success

  11. 11

    jQuery/Ajax success function not rendering html

  12. 12

    Ajax: How to pass variable one function to another jQuery

  13. 13

    Repeating jQuery Ajax success function

  14. 14

    jQuery Ajax success callback not firing nor returning error

  15. 15

    how to read javascript variable from ajax success callback function

  16. 16

    How to pass $(this) in success callback of $.ajax

  17. 17

    Pass a socket io connection to an ajax success function

  18. 18

    how to pass a variable to a Page Method Success callback function

  19. 19

    JQuery Ajax Type error in success callback

  20. 20

    AJAX success callback only carrying out first function

  21. 21

    Jquery Ajax Callback External Function

  22. 22

    JQuery ajax success fired twice, because of .done callback

  23. 23

    jquery ajax success not finding variable div

  24. 24

    jQuery Custom Event not triggering in AJAX Success Callback

  25. 25

    Ajax: How to pass variable one function to another jQuery

  26. 26

    how to read javascript variable from ajax success callback function

  27. 27

    jQuery - Use variable in AJAX request for it's success callback

  28. 28

    jquery ajax success redirect as a variable url

  29. 29

    Nested AJAX requests without a callback success function

HotTag

Archive