递归函数无法检查站点的生成索引(desc,标题,img等,存储在我的数据库中)

问题描述 投票:0回答:1
var infiniteResponseAAA= {
Poodwaddle_com: '"Poodwaddle.com", infiniteResponse[1].children[language].children[17].innerHTML, "Images\poodwaddle.com.jpg", searchBResponse[1].children[17].textContent',
SystemRequirementsLab_com: '"System Requirements Lab.com", infiniteResponse[1].children[language].children[18].innerHTML, "Images\systemrequirementslab.com.jpg", searchBResponse[1].children[18].textContent',
BrowsePad: '"BrowsePad.org", infiniteResponse[1].children[language].children[19].innerHTML, "Images\BrowsePad.org.jpg", searchBResponse[1].children[19].textContent',
SMMRY_com: '"SMMRY.com", infiniteResponse[1].children[language].children[20].innerHTML, "Images\smmry.com.jpg", searchBResponse[1].children[20].textContent',
Whatthefuckshouldimakefordinner_com: '"Whatthefuckshouldimakefordinner.com", infiniteResponse[1].children[language].children[21].innerHTML, "Images\Whatthefuckshouldimakefordinner.com.jpg", searchBResponse[1].children[21].textContent',
Coolors_co: '"Coolors.co", infiniteResponse[1].children[language].children[22].innerHTML, "Images\Coolors.co.jpg", searchBResponse[1].children[22].textContent',
Numbeo_com: '"Numbeo.com", infiniteResponse[1].children[language].children[23].innerHTML, "Images\Numbeo.com.jpg", searchBResponse[1].children[23].textContent',
Doesthedogdie_com: '"Doesthedogdie.com", infiniteResponse[1].children[language].children[24].innerHTML, "Images\Doesthedogdie.com.jpg", searchBResponse[1].children[24].textContent',
Online_Convert_com: '"Online-Convert.com", infiniteResponse[1].children[language].children[25].innerHTML, "Images\Online-Convert.com.jpg", searchBResponse[1].children[25].textContent'
}

var infiniteResponse= Object.keys(infiniteResponseAAA).sort();

var sitesOnPage = [];


var brLength= document.querySelectorAll("br").length;
var whereToAddIt= document.querySelectorAll("br")[brLength-1];


var observer = new IntersectionObserver(function(entries){
    entries.forEach(function(entry) {
        if (entry.isIntersecting) {
            getDataInfiniteS();
        }
    });
});
observer.observe(whereToAddIt);


// Data checker
function getDataInfiniteS() {

    InfiniteScrolling();

}

// Actual script
function InfiniteScrolling() {

    var code;

    // It gets the first 5 sites that are already on the page and puts them inside sitesOnPage
    if (sitesOnPage[0] === undefined) {
        var h2s= document.querySelectorAll("h2");
        infiniteResponseAAA[infiniteResponse[0]].split(",")[0].replace(/["]/g, "");

        for (var i=0; i < h2s.length; i++) { 

            if (h2s[i].innerHTML === infiniteResponseAAA[infiniteResponse[i]].split(",")[0].replace(/["]/g, "")) sitesOnPage.push(i);

        }
    }


    function randomSiteInfosF(siteN) {

        // Checks if it's a number and not a string or something else
        if (typeof siteN !== "number") return;


        siteChecker_RNG(siteN)


        // Infos about the site: title              
        eval(`siteInfo${siteN}= infiniteResponseAAA[infiniteResponse[${eval(`randomSite${siteN}`)}]].split(",")`);
        eval(`siteTitle${siteN}= siteInfo${siteN}[0].replace(/["]/g, "")`);

    }


    function siteChecker_RNG(siteN) {

        var sitesAvailable= infiniteResponse.length;
        var h2s= document.querySelectorAll("h2");
        var randomSiteN= Math.floor(Math.random() * sitesAvailable);

        //  Return if all sites are loaded
        if (h2s.length === sitesAvailable) return;


        // Checks if the random number is a correct one
        for (var i= 0; i < sitesOnPage.length; i++) {

            if (randomSiteN === sitesOnPage[i]) {

                siteChecker_RNG(siteN)

            } else continue;

        }

        eval(`randomSite${siteN}= randomSiteN`);

    }


    var howManySitesPerLoad= 3;


    for (let i= 0; i < howManySitesPerLoad; i++) {
        randomSiteInfosF(i);
    }



    // Creates a new div for the sites loaded from the database
    if (document.getElementById("InfiniteScrollingID") === null) {

        whereToAddIt.insertAdjacentHTML("afterend", "<div id='InfiniteScrollingID'></div>");

    }


    for (let i= 0; i < howManySitesPerLoad; i++) {

        code= `${eval(`siteTitle${i}`)}\n</br></br></br>`

        document.getElementById("InfiniteScrollingID").insertAdjacentHTML("beforeend", code);

    }

    for (var i=0; i < howManySitesPerLoad; i++) {

        sitesOnPage.push(eval(`randomSite${i}`))

    }

    brLength= document.querySelectorAll("br").length;
    whereToAddIt= document.querySelectorAll("br")[brLength-1];

    observer.disconnect()
    observer.observe(whereToAddIt)

}

这是一个重新发布,因为在我提供一个可行的最小示例之前,没有人回答我,并且此脚本对我的网站来说非常重要。

这是我的网站http://www.coolwebsites.ml/部分中用于我的网站的代码的一部分>

https://youtu.be/QDrn_9svt6Y

代码笔:https://codepen.io/Attisalva/pen/pojdZXr?editors=1010

在CodePen示例中(相同的问题),它会创建(在本例中为标题名称,以简化代码)页面上已经存在的一些标题名称,并且它应该仅创建尚未创建的标题(您可以找到标题在对象“ infiniteResponseAAA”中)。

我正试图懒惰地在页面上随机加载新图像,标题,descs等,但是它会继续创建而不会正确检查页面上是否已存在所有内容。

var infiniteResponseAAA = {Poodwaddle_com:'“ Poodwaddle.com”,infiniteResponse [1] .children [language] .children [17] .innerHTML,“ Images \ poodwaddle.com.jpg”,searchBResponse [1] .children [17 ] .textContent',...

javascript recursion random lazy-loading
1个回答
0
投票

这是因为我得到一个好的数字后没有结束/返回该函数

© www.soinside.com 2019 - 2024. All rights reserved.