-1

this is the code I have, I want to increment the value of the variable sfrom everytime the function is called, the function is triggered when someone scrolls to the bottom of the page.

any idea please ?

<script type="text/javascript">

if (typeof sfrom=="undefined") {
    var sfrom = 0;}
$(function(){

    $('.tainer').scrollPagination(
    {
        'contentPage': 'listitem.php', 
        'contentData': {startfrom:sfrom},  
        'scrollTarget': $(window), 
        'heightOffset': 5, 
        'beforeLoad': function(){

        },
        'afterLoad': function(elementsLoaded){ 
             var i = 0;
             $(elementsLoaded).fadeInWithDelay();

        }
    });   
});

cppit
  • 4,478
  • 11
  • 43
  • 68

2 Answers2

2

Edit: removed note about being out of scope - wasn't aware of javascript's scoping rules - notes here for the interested: https://stackoverflow.com/a/61173/117215.

This still applies however:

Is there a reason why you can't just do this?

var sfrom = 0;

$(function(){

    $('.tainer').scrollPagination(
    {
        'contentPage': 'listitem.php', 
        'contentData': {startfrom:sfrom},  
        'scrollTarget': $(window), 
        'heightOffset': 5, 
        'beforeLoad': function(){

        },
        'afterLoad': function(elementsLoaded){ 
             var i = 0;
             $(elementsLoaded).fadeInWithDelay();
             sfrom++;
        }
    });   
});
Community
  • 1
  • 1
Paddy
  • 33,309
  • 15
  • 79
  • 114
  • 2
    if `sfrom` might already exist, and in that case shouldn't be overwritten, another solution would be `var sfrom = sfrom || 0` – David Hedlund Aug 10 '12 at 09:46
  • that was not my question unfortunately :/. I am trying to increment the variable sfrom every time the function is triggered that way it displays the next result. – cppit Aug 10 '12 at 09:50
  • I put it the same way its showed by Paddy, but the value is not incremented. when the ajax request is sent it shows on firebug console "0" meaning its not being incremented... – cppit Aug 10 '12 at 09:57
  • @Paddy : that didnt work unfortunately.it still returns 0 as sfrom value – cppit Aug 10 '12 at 10:00
  • What exactly is it that scrollpagination does? – Paddy Aug 10 '12 at 10:55
  • 1
    An `if' statement doesn't have any scope. Javascript has function scope only, not block scope. So, the putting it in the `if` statement is the SAME scope as putting it at the top level. – jfriend00 Aug 10 '12 at 13:53
  • 1
    @jfried00 - I did not know that. Cheers. Learnt something important there today. – Paddy Aug 10 '12 at 14:04
0

ContentData is static in this case. Instead of inserting a variable insert a function like this answer https://stackoverflow.com/a/8581440/96593 or something similar to this

var sfrom = 0;

$(function(){

$('.tainer').scrollPagination(
{
    'contentPage': 'listitem.php', 
    'contentData': {startfrom:function() {return sfrom++;}},
    'scrollTarget': $(window), 
    'heightOffset': 5, 
    'beforeLoad': function(){

    },
    'afterLoad': function(elementsLoaded){ 
         var i = 0;
         $(elementsLoaded).fadeInWithDelay();
         sfrom++;
    }
});   

});

Community
  • 1
  • 1
Josh C
  • 7,461
  • 3
  • 24
  • 21