I have the following code in my program, which is supposed to run through my devices, and test if $.get() gives a result or fails, but I notice with these console logs that it only does that for the last one in the loop.
Logs for 2 devices in the loop:
before get: efbekmtfgjyz9pr_0_0
before get: gu2u1hnspgo2vhk_1_0
done get: gu2u1hnspgo2vhk_1_0
always get: gu2u1hnspgo2vhk_1_0
The code:
var deviceIx = 0;
while (deviceIx < service.devices.length) {
var devId = buildUniqueHTMLId(
device.jid + "_" + deviceIx + "_" + serviceIx
);
console.log("before get: " + devId);
$.get(device.thumbnail).done(function () {
console.log("done get: " + devId);
$('.devicelist #deviceImg-' + devId).attr("src", device.thumbnail);
var img = device.thumbnail;
var caption = "FRONT";
$("#deviceImagesCarousel .carousel-inner").append("<div class='carousel-item'><img class='d-block w-100' src='" + img + "' alt='" + img + "'><div class='carousel-caption'>" + caption +"</div></div>");
$("#deviceImagesCarousel .carousel-indicators").append("<li data-target='#deviceImagesCarousel' data-slide-to='0' ></li>");
$('#deviceImagesCarousel .carousel-inner .carousel-item').first().addClass('active');
$('#deviceImagesCarousel .carousel-indicators li').first().addClass('active');
}).fail(function () {
console.log("fail get: " + devId);
$('.devicelist #deviceImg-' + devId).attr("src", 'script/iopsys/devicemanager/images/hardwareversions/gateway/na_t.png');
$("#deviceImagesCarousel .carousel-inner").append("<div class='carousel-item'><img class='d-block w-100' src='script/iopsys/devicemanager/images/hardwareversions/gateway/na_t.png' alt='First slide'></div>");
$("#deviceImagesCarousel .carousel-indicators").append("<li data-target='#deviceImagesCarousel' data-slide-to='0' ></li>");
$('#deviceImagesCarousel .carousel-inner .carousel-item').first().addClass('active');
$('#deviceImagesCarousel .carousel-indicators li').first().addClass('active');
}).always(function() {
console.log("always get: " + devId);
$('.devicelist #deviceImg-' + devId).attr("src", device.thumbnail);
});
If anyone can explain why this happens, and does not log for the first device in this example, then I would be very happy.