Jump to content
bubbles

Problema functie javascript

Recommended Posts

Posted

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.

Posted

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. 

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...