0

I'm making a simple comments page for a mobile site. The trouble I have is I am trying to make my fixed header show what posts are showing. so 1-6 on the first page, 7-12 on the next.

This is all to be triggered with a swipe left or right to the next page will add 6 to the variables.

The trouble I have is the variables are resetting to their original set value.

How do I get the variables to add and decrease with the 2 functions without resetting.

$(document).on('pageinit', function(event){

var currentStart = 1;
var currentEnd = 6;

  $('div.ui-page').on("swipeleft", function () {
    var nextpage = $(this).next('div[data-role="page"]');
      if (nextpage.length > 0) {
        $.mobile.changePage(nextpage, { transition: "slide"},"slide", false, true);
        currentStart += 6;
        currentEnd += currentEnd + 6;
        $('#currentStart').empty().append(currentStart);
        $('#currentEnd').empty().append(currentEnd);

      }
  });

  $('div.ui-page').on("swiperight", function () {
    var prevpage = $(this).prev('div[data-role="page"]');
    if (prevpage.length > 0) {
      $.mobile.changePage(prevpage, { transition: "slide", reverse: true }, true, true);
      currentStart = currentStart - 6;
      currentEnd = currentEnd - 6;
      $('#currentStart').empty().append(currentStart);
      $('#currentEnd').empty().append(currentEnd);
    }
  });

So every time you swipe left or right the currentStart will increment up by 6 or decrease by 6, however everytime you swipe the variables are resetting to their original declaration.

How do I stop that?

3
  • 1
    I see that you are setting the variable values to their defaults within a pageinit function. Maybe if you move those statements outside the pageinit all would be well?
    – Kolban
    Nov 11, 2014 at 21:52
  • 1
    $(document).on('pageinit', function(event){ this will fire on each and every page but one time per each. Each time you visit a "new page" you run the same code. you need to delegate it to specific page(s) $(document).on('pageinit', "#pageID", function(event){
    – Omar
    Nov 11, 2014 at 21:52
  • Thanks guys, I'm new to Jquery mobile and scrambling something together. Much appreciated it works great now. Nov 12, 2014 at 7:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.