bubbles Posted December 2, 2017 Report Posted December 2, 2017 Salut, Am urmatoarea functie javascript: function generate_pic_unit2(idunit,category) { var treeObj; $.ajax({ url: "json.php?idunit=" + idunit + "&category=" +category, type: "GET", dataType: "json", success: function(datajson) { treeObj = datajson; var clickedID = 1; var count = treeObj.galerie.length; var firstIndex = 0, lastIndex = 0; $.each(treeObj.galerie, function(i, v) { var index = i; if (index == 0) firstIndex = v.id; if (index == count - 1) lastIndex = v.id; }); $(".id" + idunit + "-next").click(function() { clickedID++; if (clickedID > lastIndex) { clickedID = firstIndex; } $(".id" + idunit + "-pic").attr("src", treeObj.galerie[clickedID - 1].img); }); $(".id" + idunit + "-prev").click(function() { clickedID--; if (clickedID < firstIndex) { clickedID = lastIndex; } $(".id" + idunit + "-pic").attr("src", treeObj.galerie[clickedID - 1].img); }); }, error: function() { $(".id" + idunit + "-next").hide(); $(".id" + idunit + "-prev").hide(); } }); json.php {"galerie":[{"id":"1","img":"https://site.ro/poza1.jpg"},{"id":"2","img":"https://site.ro/poza2.jpg"},{"id":"4","img":"https://site.ro/poza3.jpg"},{"id":"4","img":"https://site.ro/poza4.jpg"}]} HTML <div class="text-center poza-alista-style"> <img class="id196-pic" src="POZA.JPG" alt=""> <div class="swiper-button-next id196-next"></div> <div class="swiper-button-prev id196-prev"></div> </div> <script>generate_pic_unit2(196,'foto_video');</script> Cand apas pe id196-next imi afiseaza urmatoarea poza din json, iar cand apas id196-prev imi afiseaza poza cu id-1. Problema este ca uneori scriptul are lag, mai ales cand apas pentru prima oara. Ce ar putea avea? Multumesc. Quote
radustefan Posted December 2, 2017 Report Posted December 2, 2017 Not an expert, dar cred ca in primul rand tu la fiecare request catre url: "json.php?idunit=" + idunit + "&category=" +category, aplici un forEach $.each(treeObj.galerie, function(i, v) { var index = i; if (index == 0) firstIndex = v.id; if (index == count - 1) lastIndex = v.id; }); Lagul e normal sa il ai prima oara pentru ca atunci parsezi jsonul nu ai nimic cached. Quote