满足条件时停止Jquery [英] Stop Jquery when condition is met
问题描述
我正在我的Wordpress网站上使用JQuery无限滚动脚本.该脚本可以正常工作,但是一旦显示所有帖子,我就无法停止该脚本.我在自定义帖子类型上使用它,然后直接对wordpress(MySQL)数据库进行查询.
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.
我还创建了一个自定义分页以与无限滚动一起使用,并使用$Totalpages
检查可用的页面数.显示所有帖子后,$Totalpages
变量等于"0",我知道没有其他帖子可以显示,并且无限滚动应该停止工作.这是我页面中的JavaScript ...
I have also created a custom pagination to use with the infinite scroll, and use $Totalpages
to 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>
我当时想阻止jQuery继续执行
I was thinking to stop the JQuery from continuing to excecute by
if ($Totalpages == 0) { stop Jquery from executing}
,当我需要执行它来编写if ($Totalpages !=0){Execute the Javascript}
时,我将如何实现它.
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.
更新问题
好,我已经将问题更新为Ceryl的以下建议,修改了代码以适合上面的代码
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');
}?>
if语句似乎是正确的,但是当我在页面上实现它时,它不起作用.如果我将unbind选项设置为4(这是我拥有的页面总数,它将阻止JQuery进行绑定,因此我知道它可以工作).但是在实现它时却没有!
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!
有什么想法吗?
推荐答案
我检查了网站并显示此代码:
I checked the website and the show this code:
/取消绑定正常行为.需要在正常的无限滚动设置之后发生. $(window).unbind('.infscr');
/ 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');
}
顺便说一下,这是网站: http://www. infinite-scroll.com/infinite-scroll-jquery-plugin/不确定是否是同一个插件,顺便说一句...
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...
这篇关于满足条件时停止Jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!