3

My attention is to check a calculated Image URI, if it exists…

If i do this for images, that are shown as overlays:

$.get(first_url).done(function(){
  $('#any_DOM_id').append('<img id="first_image" src="'+first_url+'"/>');
}).fail(function(){
  $('#any_DOM_id').append('<img id="first_image" src="'+fallback_url+'"/>');
});

/*** HERE is something big happening and is calculated ***/
.
. 
.
. 
.
$.get(second_url).done(function(){
  $('#any_DOM_id').append('<img id="second_image" src="'+second_url+'"/>');
}).fail(function(){
  $('#any_DOM_id').append('<img id="second_image" src="'+fallback_url+'"/>');
});
.
. 
.
$.get(third_url).done(function(){
  $('#any_DOM_id').append('<img id="third_image" src="'+third_url+'"/>');
}).fail(function(){
  $('#any_DOM_id').append('<img id="third_image" src="'+fallback_url+'"/>');
});
.
. 
.
$('#any_DOM_id').append('<img id="fourth_image" src="'+fourth_url+'"/>');

my sequential Chronology is broken, so the images are not in the right position at the layers. Have I to use $.get on all images, or does $.get kills the Chronology always?

EDIT:

This is what i'am doing: enter image description here

the images have to be in order like this:

<style>
  .image_wrapper{
    position:relative;
  }
  .image_wrapper img{
    position:absolute;
  }     
</style>
<div id="image_wrapper">
  <img id="first_image" src="first_url"/>
  <img id="second_image" src="second_url"/>
  <img id="third_image" src="third_url"/>
  <img id="fourth_image" src="fourth_url"/>
</div>
Viktor
  • 623
  • 4
  • 10
  • 25

3 Answers3

2

You can use the always callback :

$.get(first_url).done(function(){
    $('#any_DOM_id').append('<img id="my_image" src="'+first_url+'"/>');
}).fail(function(){
    $('#any_DOM_id').append('<img id="my_image" src="'+fallback_url+'"/>');
}).always(function(){
    $('#any_DOM_id').append('<img id="my_image" src="'+second_url+'"/>');
    $('#any_DOM_id').append('<img id="my_image" src="'+third_url+'"/>');
    $('#any_DOM_id').append('<img id="my_image" src="'+fourth_url+'"/>');
});

Add handlers to be called when the Deferred object is either resolved or rejected.

jQuery .always()


Edit

You can create a function with a callback. The callback receive the url (fallback or good one) That function receive an array of URL and do the requests in order :

checkURI([first_url, second_url, third_url, fourth_url], function(src){
    $('#any_DOM_id').append('<img id="my_image" src="'+src+'"/>');
})

function checkURI(uri, callback){
    get(0)
    function get(i){
        if(typeof uri[i] !== 'undefined')
            $.get(uri[i])
            .done(function(){
                callback(uri);
                get(i++);
            }).fail(function(){
                callback('YOUR FALLBACK URL');
                get(i++);
            });
    }
}
Community
  • 1
  • 1
Karl-André Gagnon
  • 33,662
  • 5
  • 50
  • 75
2

Using .reduce() like a boss

[first_url, second_url, third_url, fourth_url].reduce(function(promise, url) {
    return promise.then(function() {
        return $.get(url);
    }).then(function(urlContents) {
        $('#any_DOM_id').append('<img id="my_image" src="'+url+'"/>');
    }).then(null, function() {
        $('#any_DOM_id').append('<img id="my_image" src="'+fallback_url+'"/>');
        return $().promise();
    });
}, $().promise());
Esailija
  • 138,174
  • 23
  • 272
  • 326
0

or maybe as a check_function inside an if?!?

if(check_uri($uri)){
  $('#any_DOM_id').append('<img id="my_image" src="'+first_url+'"/>');
}else{
  $('#any_DOM_id').append('<img id="my_image" src="'+fallback_url+'"/>');
}

var check_uri = function($uri){
  $.get($uri).done(function(){
    return true;
  }).fail(function(){
    return false;   
  });
};
Viktor
  • 623
  • 4
  • 10
  • 25