0

I have a project running on codeigniter where I am using a jscroll for infinite scrolling. I was using a js file called rotator.js to flash comments on each of the posts. When my home controller runs index function it calls a loadpost() function in the same controller which calls the posts view. the home controller is as follows

$count = 2;     
$pg = 1;

    $this->load->view('header');
    $this->load->view('nav', $data);
    $this->load->view('login', $data);

    $this->loadpost($pg,$count);
    $this->load->view('footer');

The loadpost() function takes the parameters $pg-> page number and $count-> number of posts per page. function calls the posts.php view which display each posts returned by it one below the other and the end of the posts.php view has the anchor link used by the jscroll as follows

<div class='pager'>     
    <a href="<?php $pg++;
                echo base_url('index.php/home/loadpost/' . $pg) ?>" class='next-selector'></a>      
</div>

Each of the posts has multiple comments which display one by one and are managed by the rotate.js file which has the following code

(function($){
$.fn.extend({ 
    rotaterator: function(options) {

        var defaults = {
            fadeSpeed: 500,
            pauseSpeed: 500,
            child:null
        };

        var options = $.extend(defaults, options);

        return this.each(function() {
              var o =options;
              var obj = $(this);                
              var items = $(obj.children(), obj);
              items.each(function() {$(this).hide();})
              if(!o.child){var next = $(obj).children(':first');
              }else{var next = o.child;
              }


              $(next).fadeIn(o.fadeSpeed, function() {
                        $(next).delay(o.pauseSpeed).fadeOut(o.fadeSpeed, function() {
                            var next = $(this).next();
                            if (next.length == 0){
                                    next = $(obj).children(':first');
                            }
                            $(obj).rotaterator({child : next, fadeSpeed : o.fadeSpeed, pauseSpeed : o.pauseSpeed});
                        })
                    });
            });
        }
    });
})(jQuery);

 $(document).ready(function() {
        $('.rotate').rotaterator({fadeSpeed:500, pauseSpeed:2000});
 });

Now, if I include the rotate.js file in the header, the comments aren't able to flash at all from the second page post onwards(in this case the third post onwards) and when i place the same script in the posts.php view it gets called multiple times with each page load and hence has an erratic behavior in the previous posts, e.g if i load three pages, the first two pages call the function multiple times and show undesired results(flashing faster and displayiong more than one comment at a time) whereas the comments in the third page posts flash properly.

What could I be doing wrong here. If the page is calling the script multiple times when placed thcript in the view then why isn't it able to use the script when it is called from the header. Any help would be appreciated.

5
  • I guess you could just use removeClass so it won't affect old posts which got processed. Commented Nov 23, 2015 at 14:10
  • @ahmad but then why would it cause an issue while calling the script from the header. Commented Nov 23, 2015 at 21:31
  • @ahmad also all comments in previous posts will stop flashing and list out if i do that Commented Nov 23, 2015 at 21:44
  • @ahmad If it helps, there is no issue with the rotator code. As it works when jscroll isn't used. I have tried a couple of different ways to flash the comments but all methods start getting inconsistent once i scroll down and load more Commented Nov 24, 2015 at 0:34
  • I understand, You're binding it to existing elements, when loading new elements it won't work; You'll have to trigger an event from your jscroll (you can give whatever name to it) then listen to that event & apply rotator to elements. Commented Nov 24, 2015 at 13:08

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.