Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loop with recursive javascript functions

I am working on a back-office for a photographer, where the photos would be in albums with sub-categories.
I am using JavaScript in the album creation page to add a new .photo block (with its HTML content).
The issue is that I apparently get an infinite loop with the function albums.addPhoto. Here is my JS code (albums.addPhoto is called by onclick attribute and calls albums.createBlock):

    var albums =  {
    createBlock: function (tag, attributes, text, elemID, elemCLASS) {
        var block = document.createElement(tag);
        switch (tag) {
            case "label":
            block.setAttribute('for', attributes[0]);
            break;
            case "input":
            block.setAttribute('type', attributes[0]);
            block.setAttribute('name', attributes[1]);
            if (attributes[0] == "text") {
                block.setAttribute('placeholder', attributes[2]);
            } else {
                block.setAttribute('value', attributes[2]);
            }
            break;
            default:

        }

        if (text) {
            block.textContent = text;
        }
        if (elemID) {
            block.id = elemID;
        }
        if (elemCLASS) {
            block.classList.add(elemCLASS);
        }

        return block;
    },
    addPhoto: function (currentSsCat, requiredPhotoID) {
        var elem = albums.createBlock("div", null, false, ("photo_" + currentSsCat + "_" + requiredPhotoID), "photo");
        document.querySelector('#ss_cat_1').appendChild(elem);

        elem.appendChild(albums.createBlock("label", ["file_" + currentSsCat + "_" + requiredPhotoID], "Importez le fichier photo", false, false));
        elem.appendChild(albums.createBlock("input", ["file", ("file_" + currentSsCat + "_" + requiredPhotoID), "Votre photo"], false, false, false));
        elem.appendChild(albums.createBlock("label", ["ref_" + currentSsCat + "_" + requiredPhotoID], "Référence de la photo", false, false));
        elem.appendChild(albums.createBlock("input", ["text", ("ref_" + currentSsCat + "_" + requiredPhotoID), "Exemple: 20160811_CLS_0005"], false, false, false));
        elem.appendChild(albums.createBlock("label", ["desc_" + currentSsCat + "_" + requiredPhotoID], "Description de la photo", false, false));
        elem.appendChild(albums.createBlock("input", ["text", ("desc_" + currentSsCat + "_" + requiredPhotoID), "Exemple: Paolo FABBRI et Lenas Drawin His Gun (ITA)."], false, false, false));

        document.querySelector("#add_photo_" + currentSsCat).onclick = albums.addPhoto(currentSsCat, (requiredPhotoID + 1));
    }
};

Then you have the HTML part (I only give you the form part, I can send the whole page but it would be useless I think) :

<form action="index.html?page=aa" method="post">
<!-- SOUS CATEGORIE -->
        <div class="ss_cat" id="ss_cat_1">
            <label for="ss_cat_1_titre">Titre :</label>
            <input type="text" name="ss_cat_1_titre" placeholder="Titre de la sous-catégorie...">

            <!-- PHOTOS DE LA CATEGORIE -->
            <div class="photo" id="photo_1_1">
                <label for="file_1_1">Importez le fichier photo :</label>
                <input type="file" name="file_1_1" value="Votre photo">
                <label for="ref_1_1">Référence de la photo :</label>
                <input type="text" name="ref_1_1" placeholder="Exemple: 20160811_CLS_0005">
                <label for="desc_1_1">Description de la photo :</label>
                <input type="text" name="desc_1_1" placeholder="Exemple: Paolo FABBRI et Lenas Drawin His Gun (ITA).">
            </div>
        </div>


        <!-- This is the link calling the function albums.addPhoto -->
        <a href="#" class="btn" id="add_photo_1" onclick="albums.addPhoto(1, 2)">Ajouter une photo</a>
    </div>
    <a href="#" class="btn" id="add_ss_cat">Ajouter un sous-catégorie</a>
    <input type="submit" name="submit" value="Ajouter le nouvel album">
</form>

Testing : The function does its job, but with an infinite loop (browser crashes or ask me if I want to stop the script, which shows a huge number of .photo blocks have been created).
On Chrome I get the message maximum call stack size exceeded and on Firefox I get too much recursion.

PS: This is my first attempt to create a back-office, if you have any suggestion to alternative methods, feel free. Thanks for reading and for your help.

like image 211
AymDev Avatar asked Jul 08 '26 20:07

AymDev


1 Answers

This is your issue:

 document.querySelector("#add_photo_" + currentSsCat).onclick = albums.addPhoto(currentSsCat, (requiredPhotoID + 1));

You are unconditionally calling albums.addPhoto within albums.addPhoto so there is infinite recursion. You probably want this instead:

document.querySelector("#add_photo_" + currentSsCat).onclick = function () { albums.addPhoto(currentSsCat, (requiredPhotoID + 1)); };

That way you set the onclick handler to a function, not the result of the function (which is undefined and doesn't make sense anyway) and it only runs when there is a click event.

like image 79
djd0 Avatar answered Jul 11 '26 11:07

djd0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!