Pass variable to function in jquery AJAX success callback -
i trying preload images jquery ajax call, having real problems passing (url) string function within success function of ajax call (if makes sense).
here code stands:
//preloader images on gallery pages window.onload = function() { settimeout(function() { var urls = ["./img/party/"]; //just 1 started ( var = 0; < 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 (failed) attempt @ passing url through .each()
call - url
ends takes value of increasing integers. not sure why or these relate to, maybe number of jpg files?
...anyway, should of course take single value in original urls array.
thanks - seem in bit of twist these callbacks.
progess?
so, mucked around bit, taking heed of comments @ron tornambe , @pisquared , here:
//preloader images on gallery pages window.onload = function() { var urls = ["./img/party/","./img/wedding/","./img/wedding/tree/"]; settimeout(function() { ( var = 0; < 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 image_link(data, i)
here there , everywhere (in each nested function etc.) same result: value i
ever logged 3. suspect because references i
point same thing , time asynchronous task gets image_link(data, i)
for...
loop on , done (and hence has value of 3). needless gives urls[i]
'undefined'.
any (more) tips how can round this?
i experiencing similar issue, , believe have found solution.
since settings object tied ajax call, can add in indexer custom setting, can access using this
in success callback:
//preloader images on gallery pages window.onload = function() { var urls = ["./img/party/","./img/wedding/","./img/wedding/tree/"]; settimeout(function() { ( var = 0; < 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 trick.
Comments
Post a Comment