SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question jQuery pagination - cycle every 10 seconds?

    Can someone help me make this pagination auto cycle every 10 seconds?
    demo: http://web.enavu.com/demos/paginate/

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Okay, in order for it to cycle it should probably return to page 1 after page 10 right?
    In order to be able to do that change:

    Code:
    function next(){
    	new_page = parseInt($('#current_page').val()) + 1;
    	//if there is an item after the current active link run the function
    	if($('.active_page').next('.page_link').length==true){
    		go_to_page(new_page);
    	}
    	
    }
    to

    Code:
    function next(){
    	new_page = parseInt($('#current_page').val()) + 1;
    	//if there is an item after the current active link run the function
    	if($('.active_page').next('.page_link').length==true){
    		go_to_page(new_page);
    		return true;
    	}
    	return false;
    }
    (add the lines in blue)

    Next, add a simple function called cycle:

    Code:
    function cycle()
    {
       if (!next()) set_page(1);
       setTimeout('cycle', 10000);
    }
    And add this to the bottom of the script

    Code:
    setTimeout('cycle', 10000);
    All done

    not tested
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ScallioXTX,

    That doesn't seem to work. It doesn't bring up any errors.. any ideas why that doesn't work?

  4. #4
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    I am trying another method
    Code:
    setInterval("next()", 3000);
    	$("#page_navigation a").click(function(){
    		//if clicked stop rotating 
    		clearInterval(inter);
    	});
    The setInterval doesn't stop when a pagination is clicked on.. Also how can I setInterval on previous when next() reaches the last pagination?

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Try this:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    	<title>jQuery pagination</title>
    	<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    	<script type="text/javascript" src="js/jquery.js"></script>
    	<script type="text/javascript">
    		$(document).ready( function() {
    			var
    			pages=$('.page'),
    			pager=$('#Pager'),
    			numPages=pages.size(),
    			currentPage=0,
    			interval,
    			timeout;
    
    			function next()	{
    				var newPage=(currentPage+1)%numPages;
    				gotoPage(newPage);
    			}
    
    			function prev() {
    				var newPage=currentPage-1;
    				if (currentPage <= 0) {
    					newPage=numPages-1;
    				}
    				gotoPage(newPage);
    			}
    
    			function cycle() {
    				interval=setInterval(next, 10000);
    			}
    
    			function gotoPage(page) {
    				if (page != currentPage) {
    					$('.page:nth('+(currentPage)+')').hide();
    					$('#Pager a:nth('+(currentPage+1)+')').removeClass('active_page');
    				}
    				$('.page:nth('+(page)+')').show();
    				$('#Pager a:nth('+(page+1)+')').addClass('active_page');
    
    				currentPage=page;
    			}
    
    			$('.page:not(:nth('+(currentPage)+'))').hide();
    
    			$('<a>', { text: 'Prev', longdesc: 'Previous page', href: 'javascript:void(0);'}).click( function() { prev(); return false; }).appendTo(pager);
    			for (var i=0;i<numPages;i++) {
    				$('<a>', { text: i+1,	longdesc: i+1, href: 'javascript:void(0);'}).appendTo(pager);
    			}
    			$('<a>', { text: 'Next', longdesc: 'Next page', href: 'javascript:void(0);'}).click( function() { next(); return false; }).appendTo(pager);
    
    			pager.delegate('a', 'click', function() {
    				gotoPage(parseInt($(this).text(), 10)-1);
    				clearInterval(interval);
    				clearTimeout(timeout);
    				timeout=setTimeout(cycle, 60000);
    			});
    
    			gotoPage(0);
    			cycle();
    		});
    	</script>
    	<style type="text/css">
    		#Pager a, #Pager a:link, #Pager a:focus, #Pager a:hover, #Pager a:active {
    			padding:3px;
    			border:1px solid gray;
    			margin:2px;
    			color:black;
    			text-decoration:none
    		}
    		.active_page {
    			background:darkblue;
    			color:white !important;
    		}
    	</style>
    </head>
    <body>
    	<div id="content">
    		<div class="page">
    			<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
    			<p>Vestibulum consectetur ipsum sit amet urna euismod imperdiet aliquam urna laoreet.</p>
    			<p>Curabitur a ipsum ut elit porttitor egestas non vitae libero.</p>
    			<p>Pellentesque ac sem ac sem tincidunt euismod.</p>
    			<p>Duis hendrerit purus vitae nibh tincidunt bibendum.</p>
    		</div>
    		<div class="page">
    			<p>Nullam in nisi sit amet velit placerat laoreet.</p>
    			<p>Vestibulum posuere ligula non dolor semper vel facilisis orci ultrices.</p>
    			<p>Donec tincidunt lorem et dolor fringilla ut bibendum lacus fringilla.</p>
    			<p>In non eros eu lacus vestibulum sodales.</p>
    			<p>Duis ultrices metus sit amet sem adipiscing sit amet blandit orci convallis.</p>
    		</div>
    		<div class="page">
    			<p>Proin ullamcorper est vitae lorem mollis bibendum.</p>
    			<p>Maecenas congue fringilla enim, tristique laoreet tortor adipiscing eget.</p>
    			<p>Duis imperdiet metus et lorem venenatis nec porta libero porttitor.</p>
    			<p>Maecenas lacinia lectus ac nulla commodo lacinia.</p>
    			<p>Maecenas quis massa nisl, sed aliquet tortor.</p>
    		</div>
    		<div class="page">
    			<p>Quisque porttitor tellus ut ligula mattis luctus.</p>
    			<p>In at mi dolor, at consectetur risus.</p>
    			<p>Etiam id erat ut lorem fringilla dictum.</p>
    			<p>Curabitur sagittis dolor ac nisi interdum sed posuere tellus commodo.</p>
    			<p>Pellentesque quis magna vitae quam malesuada aliquet.</p>
    		</div>
    		<div class="page">
    			<p>Curabitur tempus tellus quis orci egestas condimentum.</p>
    			<p>Maecenas laoreet eros ac orci adipiscing pharetra.</p>
    			<p>Nunc non mauris eu nibh tincidunt iaculis.</p>
    			<p>Ut semper leo lacinia purus hendrerit facilisis.</p>
    			<p>Praesent et eros lacinia massa sollicitudin consequat.</p>
    		</div>
    		<div class="page">
    			<p>Proin non mauris in sem iaculis iaculis vel sed diam.</p>
    			<p>Nunc quis quam pulvinar nibh volutpat aliquet eget in ante.</p>
    			<p>In ultricies dui id libero pretium ullamcorper.</p>
    			<p>Morbi laoreet metus vitae ipsum lobortis ultrices.</p>
    			<p>Donec venenatis egestas arcu, quis eleifend erat tempus ullamcorper.</p>
    		</div>
    		<div class="page">
    			<p>Morbi nec leo non enim mollis adipiscing sed et dolor.</p>
    			<p>Cras non tellus enim, vel mollis diam.</p>
    			<p>Phasellus luctus quam id ligula commodo eu fringilla est cursus.</p>
    			<p>Ut luctus augue tortor, in volutpat enim.</p>
    			<p>Cras bibendum ante sed erat pharetra sodales.</p>
    		</div>
    		<div class="page">
    			<p>Donec sollicitudin enim eu mi suscipit luctus posuere eros imperdiet.</p>
    			<p>Vestibulum mollis tortor quis ipsum suscipit in venenatis nulla fermentum.</p>
    			<p>Proin vehicula suscipit felis, vitae facilisis nulla bibendum ac.</p>
    			<p>Cras iaculis neque et orci suscipit id porta risus feugiat.</p>
    			<p>Suspendisse eget tellus purus, ac pulvinar enim.</p>
    		</div>
    		<div class="page">
    			<p>Morbi hendrerit ultrices enim, ac rutrum felis commodo in.</p>
    			<p>Suspendisse sagittis mattis sem, sit amet faucibus nisl fermentum vitae.</p>
    			<p>Nulla sed purus et tellus convallis scelerisque.</p>
    			<p>Nam at justo ut ante consectetur faucibus.</p>
    			<p>Proin dapibus nisi a quam interdum lobortis.</p>
    		</div>
    		<div class="page">
    			<p>Nunc ornare nisi sed mi vehicula eu luctus mauris interdum.</p>
    			<p>Mauris auctor suscipit tellus, at sodales nisi blandit sed.</p>
    		</div>
    
    	</div>
    	<div id="Pager"></div>
    </body>
    </html>
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, thanks for this but I cannot use it because I can't add <div class="page"> </div> round the lists I am using.. The pagination I am using also changes it won't always be 10 pages it could be 5 one day and a week later it might change to 7 pages..

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    You can make as many pages as you like, just wrap each page in <div class="page"></div>
    The script will automatically figure out how many pages there are and built the links to the individual pages accordingly.

    What do you mean by a list? an <ul> ? You could break the <ul> apart in pieces and put each piece in it's own <div class="page">
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its one big list taken from an rss feed I can't break it apart :/

  9. #9
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Okay, in that case we'd better continue with the code you already had.

    I see I made a mistake in post #2. Please try and apply all changes there but replace

    Code:
    setTimeout('cycle', 10000);
    with

    Code:
    setTimeout(cycle, 10000);
    i.e. remove the ' around "cycle"

    Does that work?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  10. #10
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    The JS is now
    Code:
    function next(){
    	new_page = parseInt($('#current_page').val()) + 1;
    	//if there is an item after the current active link run the function
    	if($('.active_page').next('.page_link').length==true){
    		go_to_page(new_page);
    		return true;
    	}
    	return false;
    }
    function cycle()
    {
       if (!next()) set_page(1);
       setTimeout(cycle, 2000);
    }
    setTimeout(cycle, 2000);
    When I click on next it echos true in the browser and when it gets to the last item and I click next it echos false in the browser?

    This kinda works but it is not perfect.

    what does set_page(1) do?

    can the cycle return to page 1 when it hits the last page and then start cycling again?

    Why doesn't the next button work?

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Okay, remove the changes from the next() function (return true and return false).
    *sigh* this is why I 'dislike' href="javascript:doSomething()"> ....

    And then change the cycle function to this:

    Code:
    function cycle()
    {
      if($('.active_page').next('.page_link').length){
        next();
      } else {
        go_to_page(1);
      }
      setTimeout(cycle, 2000);
    }
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  12. #12
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK excuse me if I am doing this wrong but it doesn't cycle any more with the revised JS?

    Code:
    function next(){
    	new_page = parseInt($('#current_page').val()) + 1;
    	//if there is an item after the current active link run the function
    	if($('.active_page').next('.page_link').length==true){
    		go_to_page(new_page);
    
    	}
    
    }
    
    function cycle()
    {
      if($('.active_page').next('.page_link').length){
        next();
      } else {
        go_to_page(1);
      }
      setTimeout(cycle, 1000);
    }

  13. #13
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    You still need to put

    Code:
    setTimeout(cycle, 2000);
    at the end to start cycling
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  14. #14
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats awesome thanks Scallio.

    I started to piece together another way of tackling this.. This is what I've done so far..

    Code:
    var pagination = $('#page_navigation a').size();
    
    $("#page_navigation a:last-child").prev().addClass('last');
    and now in simple terms this is what I had in mind

    if pagination > 0 ( click .next_link every 10 seconds & when .last reset)

    not sure how to actually get it to work.. my jquery is not brilliant as you can see

  15. #15
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by shtoom View Post
    Thats awesome thanks Scallio.

    I started to piece together another way of tackling this.. This is what I've done so far..

    Code:
    var pagination = $('#page_navigation a').size();
    
    $("#page_navigation a:last-child").prev().addClass('last');
    and now in simple terms this is what I had in mind

    if pagination > 0 ( click .next_link every 10 seconds & when .last reset)

    not sure how to actually get it to work.. my jquery is not brilliant as you can see
    Ah, but that is exactly what my cycle function does (the one in #11). Take a closer look at it
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  16. #16
    SitePoint Addict
    Join Date
    Dec 2006
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    Ah, but that is exactly what my cycle function does (the one in #11). Take a closer look at it
    do you mean line 11?

  17. #17
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,904
    Mentioned
    139 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by shtoom View Post
    do you mean line 11?
    No, I mean post number 11 of this thread. Sorry, should have made that more clear
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •