Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

javascript - Stop Jquery when condition is met

I am using the JQuery infinite scroll script on my Wordpress website. The script works fine, but I cannot get the script to stop once all posts are shows. I am using it on a custom post type, and making a query directly to the wordpress (MySQL) database.

I have also created a custom pagination to use with the infinite scroll, and use $Totalpagesto check how many pages are available. Once all the Posts have been shown the $Totalpages variable equals "0" and I know that there is no more posts available to show and infinite scroll should stop working. Here is the JavaScript inside my page...

<script>
    var infinite_scroll = {
        loading: {
            img: "<?php echo get_template_directory_uri(); ?>/images/ajax-loader.gif",
            msgText: "<?php _e( 'Loading the next set of posts...', 'custom' ); ?>",
            finishedMsg: "<?php _e( 'All posts loaded.', 'custom' ); ?>"
        },
        "nextSelector":".previous a",
        "navSelector":".post-nav",
        "itemSelector":"article",
        "contentSelector":".main"
    };
    jQuery( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );
    </script>

I was thinking to stop the JQuery from continuing to excecute by

if ($Totalpages == 0) { stop Jquery from executing} and when I need to execute it to write if ($Totalpages !=0){Execute the Javascript} how will I go about implementing this.

UPDATE TO QUESTION

Ok I have updated my problem to what Ceryl suggested below, modifying the code to fit the code above

<?php if ($total_pages == 0) { 
  echo 'jQuery(window).unbind( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );';
}?>

<?php if ($total_pages != 0){
   echo 'jQuery(window).bind( infinite_scroll.contentSelector ).infinitescroll( infinite_scroll );';
   //$(window).unbind('.infscr');

}?>

The if statements seem correct but when I implement it on my page it does not work. If I set the unbind option to 4 (which is the total number of pages I have it stops the JQuery from binding and so I know it works). But when implementing it, it does not!

Any ideas why this is?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I checked the website and the show this code:

/ unbind normal behavior. needs to occur after normal infinite scroll setup. $(window).unbind('.infscr');

So I guess you can just do that to disable the behaviour:

if ($Totalpages == 0) { 
   $(window).unbind('.infscr');
}

($Totalpages !=0){
   $(window).bind('.infscr');
}

By the way, this was the website: http://www.infinite-scroll.com/infinite-scroll-jquery-plugin/ not sure if it's the same plugin, btw...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...