i think this has something to with the function being asynchronous but i’m not sure. i think it’s a quick answer but i can’t figure it out.
i’m trying to check if images exist on the server. if they do, then set a variable. if they don’t then move on and check the next image.
every time i alert the variable though i keep getting 0. i know the urls are correct and everything. for some reason though i can’t get the variable. any tips?
$('.rightPostContent img').each(function(){
var c = $(this).attr('src');
var pot1 = c.replace('_B.png','_pt1.gif');
var pot2 = c.replace('_B.png','_pt2.gif');
var pot3 = c.replace('_B.png','_pt3.gif');
$.get(pot3)
.done(function() {
var t = 3;
}).fail(function() {
})
$.get(pot2)
.done(function() {
var t = 2;
}).fail(function() {
})
$.get(pot1)
.done(function() {
var t = 1;
}).fail(function() {
})
alert(t);
});
the variable t wont alert anything, even though all pot images exist in this example.
Exactly…
This is asynchronous call… So when the control reaches your
alertstatement, the$.get()have not received the data from the server or whatever source yet.
If you need to use that set value of
t, you might wanna create a new function where thistneeds to be passed. And then call that function from yourdone()orfail()call backs.