JavaScript Variable not incrementing in jQuery / JS? variable increment jquery,increment variable value in jquery,jquery variable

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 );

EDIT: Thanks for your responses so far! Updated code below:

<script type="text/javascript">
    pageCount = 2;
    total = <?php echo $loop->max_num_pages; ?>;
    jQuery( window ).scroll( function() {
        if  ( jQuery( window ).scrollTop() == jQuery( document ).height() - jQuery( window ).height() ){
            console.log( 'Old value: ' + pageCount );
            if ( pageCount > total ){
                return false;
            } else {
                loadArticle( pageCount );
            }
            pageCount++;
            console.log( 'New value: ' + pageCount );
        }
    }); 

    function loadArticle( pageNumber ) {
        jQuery.ajax({
            url: "<?php bloginfo( 'wpurl' ) ?>/wp-admin/admin-ajax.php",
            type:'POST',
            data: "action=infinite_scroll&page_no=" + pageNumber + '&loop_file=forums', 
            success: function( html ){
                jQuery( "#content" ).append( html );
            }
        });
    return false;
    }
</script>

New value is always 3 and old value is always 2 (output in console) so it's still being reset...

SOLVED: The html callback in the ajax method was causing the issue. Moving the increment into there worked! New code:

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

two things first:

var can be shortened as such:

var var1 = 'string',
    var2 = 'int';

Don't mix php inside JS - it's messy. Create a hidden span and assign data tags to it and call like that

secondarily, that's because the value gets reset whenever you call the function. Place outside, or pass a parameter called count so you can make it more generic and use it throughout your project.

Answer:2

Define count variable outside the function.

e.g.

var count = 2;
( function( $ ) {
        $( document ).ready( function () {
            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 );
Answer:3

I have two div section were I'm performing drag/drop and sortable operations i have list of 5 draggable records in my draggable section I want to disable the draggable record as soon as I drop in ...

I have two div section were I'm performing drag/drop and sortable operations i have list of 5 draggable records in my draggable section I want to disable the draggable record as soon as I drop in ...

  1. disable draggable jquery
  2. disable draggable jquery ui
  3. enable disable draggable jquery
  4. disable draggable div jquery
  5. jquery disable draggable on element
  6. jquery disable draggable after drop

Can someone tell me why the JavaScript code below is returning an undefined error when I click on the button the second time? When clicked the first time, it will reveal the text in the h3 element, ...

Can someone tell me why the JavaScript code below is returning an undefined error when I click on the button the second time? When clicked the first time, it will reveal the text in the h3 element, ...

I recently moved from callback functions to promises in node.js. I want to preform async query to the DB (psql) in the most elegant way. I was wondering if the following code is the right way to do it ...

I recently moved from callback functions to promises in node.js. I want to preform async query to the DB (psql) in the most elegant way. I was wondering if the following code is the right way to do it ...

  1. node chain promise
  2. node promise chain error handling
  3. node promise chain example
  4. node js chain promise
  5. node js promise chain example
  6. node break promise chain
  7. node js promise chain catch
  8. node js promise chain error
  9. node js break promise chain
  10. node.js exit promise chain
  11. node.js request-promise chain

For websites that rely on Ajax or javascript to render data, how can I use WWW::Selenium to save the data? My following code was able to perform all the clicks and get to the correct webpage, however ...

For websites that rely on Ajax or javascript to render data, how can I use WWW::Selenium to save the data? My following code was able to perform all the clicks and get to the correct webpage, however ...

  1. website builder
  2. website design
  3. website maker
  4. website hosting
  5. website templates
  6. website checker
  7. website speed test
  8. website citation
  9. website builder free
  10. website name generator
  11. website down
  12. website unblocker
  13. website domain
  14. website apa citation
  15. website in spanish
  16. website archive
  17. websites like youtube
  18. website traffic
  19. website ideas
  20. websites like airbnb