For some reason my variable keeps resetting to 2 despite me wanting to increment the variable when a function is called. It’s really annoying me and I’ve refactored the code several times to no avail! It should be simple…

Here is my code:

( function( $ ) {
        $( document ).ready( function () {
            var count = 2;
            var total = <?php echo $loop->max_num_pages; ?>;
            if ( count <= total ) {
                $( window ).scroll( function() {
                    if ( $( window ).scrollTop() == $( document ).height() - $( window ).height() ) {
                        $.ajax({
                            url: "<?php bloginfo( 'wpurl' ) ?>/wp-admin/admin-ajax.php",
                            type:'POST',
                            data: "action=infinite_scroll&page_no=" + count + '&loop_file=forums', 
                            success: function( html ){
                                $( "#content" ).append( html );
                            }
                        });
                        count++;
                    }
                });
            } else {
                return;
            }
        });
    })( jQuery );

I’m pretty stuck so any help would be very much appreciated!

Cheers :)

Read more here: Variable not incrementing in jQuery / JS?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: