0

I've been trying to get this infinite scroll script to work for the past 8 hours and can't figure it out.

I uploaded the site here so you can see the problem: http://kevinellerton.com/meditationmagazine/article01/ as you scroll down, it's supposed to load article04, then 03, then 02, and then stop loading articles since article01 was loaded already. The problem is that it doesn't stop loading them... and if you track the console-logged variables in the console, it's really wonky. I think the problem might have to do with "scope" but I'm not sure :-(

Please help if you can!

Thank you so much!

$(document).ready(function() {

// DATABASE OF ARTICLES
var database = [
    "article04",
    "article03",
    "article02",
    "article01"
];



// THIS WHOLE SECTION CREATES THE INFINITE SCROLL EFFECT BY ACCESSING THE ARTICLES IN ORDER.
// IT'S NOT QUITE WORKING THOUGH.
// IF YOU FIGURE OUT THE BUGS, YOU CAN ACTIVATE INFINITE SCROLL!
// POST CODE ON STACKOVERFLOW... MAYBE YOU'LL GET SOME HELP!

// get initial page name
if (articleCounter == 0) {
    var pathArray = window.location.pathname.split('/');
    var pageName = pathArray[2];
    console.log(pageName);
}

// initialize variables
var win = $(window);
var articleCounter = 0;
console.log(articleCounter);

// Each time the user scrolls
win.scroll(function() {
    // End of the document reached?
    if ($(document).height() - win.height() == win.scrollTop()) {
        $('#loading').show();

        // if this article is the next one in the list, skip it and go to next one
        if (pageName == database[articleCounter]) {
            articleCounter++;
            var nextPage = database[articleCounter];
            pageName = nextPage;
            console.log(nextPage);
        } else {
            var nextPage = database[articleCounter];
            pageName = nextPage;
            console.log(nextPage);
        }

        // append nextPage to the end of the document
        if (nextPage !== undefined) {
            $.ajax({
                url: '../' + nextPage + '/index.php',
                dataType: 'html',
                success: function(html) {
                    $('body').append(html);
                    $('#loading').hide();
                    articleCounter++;
                        console.log(articleCounter);
                    // POSSIBLE TO CHANGE URL PATH NAME HERE???
                }
            });
        }
    }
});
});

EDIT:

I totally re-wrote it today and it's still having the exact same problems. I'm 90% sure that the problem has to do with SCOPE, but I'm not quite figuring it out. Here's my new code that's still not working:

$(document).ready(function() {

// DATABASE OF ARTICLES
var database = [
    "article04",
    "article03",
    "article02",
    "article01"
];

var articleCounter = 0;

// get initial page name
var pathArray = window.location.pathname.split('/');
var initialPage = pathArray[2];
alert("Initial page is " + initialPage);

// when you scroll
$(window).scroll(function() {
    // if you've reached the end of the document
    if ($(document).height() - $(window).height() == $(window).scrollTop()) {
        $('#loading').show();
         // if you've reached the end of the database
        if (articleCounter >= database.length) {
            // load ending footer
            alert("You've reached end of database! articleCounter is " + articleCounter);
            // quit program
        }
        else if (database[articleCounter] == initialPage) {
            articleCounter++;
            appendArticle();
            articleCounter++;
        }
        else {
            appendArticle();
            articleCounter++;
        }
    }
});

// end scroll function

function appendArticle() {
    $.ajax({
        url: '../' + database[articleCounter] + '/index.php',
        dataType: 'html',
        success: function(html) {
            $('body').append(html);
            $('#loading').hide();
            // POSSIBLE TO CHANGE URL PATH NAME HERE???
        }
    });
}

2 Answers2

0

You have defined variable in if statement, that's not ok, and I'll suppose that your logic for getting next page and counter is OK, and btw you commented almost all the code with /* */ so:

$(document).ready(function() {
//you will return to page beginning if no article, so you don't have to include article1

// DATABASE OF ARTICLES
var database = [
    "article04",
    "article03",
    "article02"
];



// initialize variables
var win = $(window);
var articleCounter = 0;
var loaded = true;
// Each time the user scrolls
win.scroll(function() {
    if ($(document).height() - win.height() == win.scrollTop()) {
        if(articleCounter == database.length){
            window.scrollTo(0,0);
            return;
        }

        if(!loaded) return
        $('#loading').show();
        loaded = false;
            $.ajax({
                url: '../' + database[articleCounter] + '/index.php',
                dataType: 'html',
                success: function(html) {
                    $('body').append(html);
                    $('#loading').hide();
                    articleCounter++;
                    loaded = true;
                } 
            });

    }
});


});
Marko Mackic
  • 2,291
  • 1
  • 8
  • 18
  • thank you so much for your quick answer! i tried your edit but it did not work :-( I uploaded the site here so you can see the problem: http://kevinellerton.com/meditationmagazine/article01/ as you scroll down, it's supposed to load article04, then 03, then 02, and then stop loading articles since article01 was loaded already. the problem is that it doesn't stop loading them... and if you track the variables in the console, it's really wonky – Kevin Ellerton Jul 31 '16 at 04:47
  • @KevinEllerton let me ask you quickly is article 1 loaded before this infinite scroll, and do you go to article 1 after all pages are scrolled – Marko Mackic Jul 31 '16 at 04:54
  • @MDMostofa no need for external plugins – Marko Mackic Jul 31 '16 at 05:04
  • @MarkoMackic yes, article 1 is loaded before the infinite scroll. you can check out the link to see how it works (you can also see all the "sources" in the browser, it's a pretty simple setup): kevinellerton.com/meditationmagazine/article01 I wouldn't be surprised if it's a problem with my logic but I spent all day trying to figure it out and couldn't do it. I'm pretty new to programming – Kevin Ellerton Jul 31 '16 at 05:35
  • @KevinEllerton I edited code, place it and see what you get :) – Marko Mackic Jul 31 '16 at 05:47
  • @MarkoMackic I see what you did there! That code might work specifically for this case (when the user starts out on the last article in the database array), but my purpose in scripting this out is to allow users to start on any article, and then when they get to the bottom of that article, the most recent one (at the top of the database array) loads, and then the next one, and the next... skipping the one they started on... and ending at the least recent article (at the bottom of the array). I plan to add hundreds of articles to the array over time. – Kevin Ellerton Jul 31 '16 at 05:57
  • I looked the source of your page, and I know what you want but then you need to enclose your articles into something like `
    your_article_html
    ` :)
    – Marko Mackic Jul 31 '16 at 06:00
  • @MarkoMackic I just tried that. Didn't work. I don't think the problem is with the HTML, I think it's with an error in my logic in the javascript, or something to do with the scope of the variables or something like that – Kevin Ellerton Jul 31 '16 at 06:10
  • No, than I can recode this.. it's currently not handling it, but I sort of lost interest, it's too much for me to do – Marko Mackic Jul 31 '16 at 06:11
0
$(document).ready(function () {

// DATABASE OF ARTICLES
var database = [
    "article04",
    "article03",
    "article02",
    "article01"
];

var win = $(window);
ArticleLoad_Counter = database.length - 1;// Assuming that article01 is initialy loaded. ie, then we need to load remaing 3 articles.
articleCounter = 0;
var pathArray = window.location.pathname.split('/');
var pageName = pathArray[2];
console.log(pageName);

$(window).scroll(function () {
    if ($(win).scrollTop() + $(window).height() == $(document).height()) {

        // Scroll bar reached at bottom 
        alert("reached bottm");
        if (ArticleLoad_Counter != 0) {
            $('#loading').show();

            if (pageName == database[articleCounter]) {
                articleCounter++;
                nextPage = database[articleCounter];
                pageName = nextPage;
                console.log(nextPage);
                LoadNewArticle(nextPage);  // Function for Ajax Call 

            } else {
                nextPage = database[articleCounter];
                pageName = nextPage;
                console.log(nextPage);
                LoadNewArticle(nextPage); // Function for Ajax call 
            }
        }
    }
});

});

   function LoadNewArticle(nextPage) {
   $.ajax({
    url: '../' + nextPage + '/index.php',
    dataType: 'html',
    success: function (html) {
        $('body').append(html);
        $('#loading').hide();
        ArticleLoad_Counter--;
        alert(nextPage);   // Loaded article name
        console.log(articleCounter);
        // POSSIBLE TO CHANGE URL PATH NAME HERE???
     }
    });

    }

Hope this code will works for your site!!

rajiv
  • 64
  • 1
  • 6
  • On your posted code u are trying to make Ajax call , when ever the scroll bar touches the bottom. This is the bug in your program. Your code itself will work if u put the Ajax Call inside the first If and else condtion. ie inside ' if (pageName == database[articleCounter]) { articleCounter++; var nextPage = database[articleCounter]; pageName = nextPage; // make ajax call here } else { var nextPage = database[articleCounter]; pageName = nextPage; // make ajax call here } ' – rajiv Aug 01 '16 at 05:31