如何在JavaScript中停止if-else语句循环

如何在JavaScript中停止if-else语句循环,javascript,loops,if-statement,Javascript,Loops,If Statement,我有这个密码 // If there is no data returned, there are no more posts to be shown. Show error if(data == "") { $this.find('.loading-bar').html($settings.error); } else { // Offset increases offset = offset+$settings.nop;

我有这个密码

// If there is no data returned, there are no more posts to be shown. Show error
if(data == "") { 

    $this.find('.loading-bar').html($settings.error);                   

} else {

    // Offset increases
    offset = offset+$settings.nop; 

    // Append the data to the content div
    $this.find('#content_ins_con_all_posts').append(data);

    // No longer busy!  
    busy = false;
}
此代码在点击底部时向我显示一条消息,并且没有其他帖子显示。我的问题是,当我继续滚动时,消息会不断显示多次。。。我只想在帖子结束时给我看一次消息。如果有一种方法我可以做到这一点是非常欢迎的

(function($) {

$.fn.scrollPagination = function(options) {

    var settings = { 
        nop     : 3, // The number of posts per scroll to be loaded
        offset  : 0, // Initial offset, begins at 0 in this case
        error   : 'No More Posts!', // When the user reaches the end this is the message that is
                                    // displayed. You can change this if you want.
        delay   : 2000, // When you scroll down the posts will load after a delayed amount of time.
                       // This is mainly for usability concerns. You can alter this as you see fit
        scroll  : true // The main bit, if set to false posts will not load as the user scrolls. 
                       // but will still load if the user clicks.
    }

    // Extend the options so they work with the plugin
    if(options) {
        $.extend(settings, options);
    }

    // For each so that we keep chainability.
    return this.each(function() {       

        // Some variables 
        $this = $(this);
        $settings = settings;
        var offset = $settings.offset;
        var busy = false; // Checks if the scroll action is happening 
                          // so we don't run it multiple times

        // Custom messages based on settings
        if($settings.scroll == true) $initmessage = 'Scroll for more or click here';
        else $initmessage = 'Click for more';

        // Append custom messages and extra UI
        $this.append('<div id="content_ins_con_all_posts"></div><div class="loading-bar">'+$initmessage+'</div>');

        function getData() {

            // Post data to ajax.php
            $.post('functions_index_result_all_img.php', {

                action        : 'scrollpagination',
                number        : $settings.nop,
                offset        : offset,

            }, function(data) {

                // Change loading bar content (it may have been altered)
                $this.find('.loading-bar').html($initmessage);

                // If there is no data returned, there are no more posts to be shown. Show error

                if(data == "") { 
                    $this.find('.loading-bar').html($settings.error);                   

                } 
                else {  
                    // Offset increases
                    offset = offset+$settings.nop; 

                    // Append the data to the content div
                    $this.find('#content_ins_con_all_posts').append(data);

                    // No longer busy!  
                    busy = false;

                }

            });

        }   

        getData(); // Run function initially

        // If scrolling is enabled
        if($settings.scroll == true) {
            // .. and the user is scrolling
            $(window).scroll(function() {

                // Check the user is at the bottom of the element
                if($(window).scrollTop() + $(window).height() > $this.height() && !busy) {

                    // Now we are working, so busy is true
                    busy = true;

                    // Tell the user we're loading posts
                    $this.find('.loading-bar').html('Loading Posts');

                    // Run the function to fetch the data inside a delay
                    // This is useful if you have content in a footer you
                    // want the user to see.
                    setTimeout(function() {

                        getData();

                    }, $settings.delay);

                }   
            });
        }

        // Also content can be loaded by clicking the loading bar/
        $this.find('.loading-bar').click(function() {

            if(busy == false) {
                busy = true;
                getData();
            }

        });

    });
}

})(jQuery);
(函数($){
$.fn.scrollPagination=函数(选项){
变量设置={
nop:3,//每个卷轴要加载的帖子数
偏移量:0,//在本例中,初始偏移量从0开始
错误:“无更多帖子!”,//当用户到达末尾时,这是所显示的消息
//已显示。如果需要,您可以对此进行更改。
延迟:2000,//当您向下滚动时,延迟一段时间后将加载帖子。
//这主要是出于可用性方面的考虑。您可以根据自己的需要进行修改
scroll:true//如果设置为false,则在用户滚动时不会加载主位。
//但如果用户单击,仍将加载。
}
//扩展选项,使其与插件一起工作
如果(选项){
$.extend(设置、选项);
}
//为每一个,使我们保持链接能力。
返回此.each(函数(){
//一些变量
$this=$(this);
$settings=设置;
var offset=$settings.offset;
var busy=false;//检查是否正在执行滚动操作
//所以我们不会多次运行它
//基于设置的自定义消息
如果($settings.scroll==true)$initmessage='滚动查看更多信息或单击此处';
else$initmessage='单击以获取更多信息';
//附加自定义消息和额外的用户界面
$this.append(“”+$initmessage+“”);
函数getData(){
//将数据发布到ajax.php
$.post('functions\u index\u result\u all\u img.php'{
操作:“滚动分页”,
编号:$settings.nop,
抵销:抵销,
},函数(数据){
//更改加载条内容(可能已更改)
$this.find('.loading bar').html($initmessage);
//如果没有返回数据,则不会显示更多的帖子。显示错误
如果(数据==“”){
$this.find('.loading bar').html($settings.error);
} 
否则{
//抵消增加
偏移量=偏移量+$settings.nop;
//将数据附加到content div
$this.find(“#content_ins_con_all_posts”).append(数据);
//不再忙了!
忙=假;
}
});
}   
getData();//最初运行函数
//如果启用了滚动
如果($settings.scroll==true){
//…并且用户正在滚动
$(窗口)。滚动(函数(){
//检查用户是否位于元素的底部
if($(窗口).scrollTop()+$(窗口).height()>$this.height()&&&!busy){
//现在我们正在工作,所以忙碌是真的
忙=真;
//告诉用户我们正在加载帖子
$this.find('.loading bar').html('loading Posts');
//运行函数以在延迟内获取数据
//如果您的页脚中有内容,这将非常有用
//希望用户看到。
setTimeout(函数(){
getData();
},$settings.delay);
}   
});
}
//也可以通过单击加载栏加载内容/
$this.find('.loading bar')。单击(function(){
如果(忙==假){
忙=真;
getData();
}
});
});
}
})(jQuery);

从我的头顶上两个方向

  • 检查光标的位置,如果它接近底部的某个范围,不要触发该代码
  • 将标志存储在sessionStorage或变量(取决于代码)中,这意味着您已经显示了消息。一旦光标离开底部超过您定义的特定距离,请删除标志

  • 两种方式从我的头顶上

  • 检查光标的位置,如果它接近底部的某个范围,不要触发该代码
  • 将标志存储在sessionStorage或变量(取决于代码)中,这意味着您已经显示了消息。一旦光标离开底部超过您定义的特定距离,请删除标志

  • 您可以在代码中设置一个标志。比如:

    // If there is no data returned, there are no more posts to be shown. Show error
    if(data == "") { 
    
        if(!flag) {
            $this.find('.loading-bar').html($settings.error);
            flag = true; // You need to declare flag = false at an appropriate place in your code.
        }
    } else {
    
        // Offset increases
        offset = offset+$settings.nop; 
    
        // Append the data to the content div
        $this.find('#content_ins_con_all_posts').append(data);
    
        // No longer busy!  
        busy = false;
    
    }
    
    请将此作为起点,而不是复制粘贴解决方案


    您可以在代码中设置一个标志。比如:

    // If there is no data returned, there are no more posts to be shown. Show error
    if(data == "") { 
    
        if(!flag) {
            $this.find('.loading-bar').html($settings.error);
            flag = true; // You need to declare flag = false at an appropriate place in your code.
        }
    } else {
    
        // Offset increases
        offset = offset+$settings.nop; 
    
        // Append the data to the content div
        $this.find('#content_ins_con_all_posts').append(data);
    
        // No longer busy!  
        busy = false;
    
    }
    
    请将此作为起点,而不是复制粘贴解决方案


    您可以使用全局变量来了解是否必须显示消息

    大概是这样的:

    var isDisplayed = false;
    
    然后


    您可以使用全局变量来了解是否必须显示消息

    大概是这样的:

    var isDisplayed = false;
    
    然后


    这段代码不应该循环,它必须是代码中重复调用它的其他东西。设置全局变量,如果底部被击中,则在其中存储值。每次加载新数据时,都将该变量设置为false。在if语句中,您将与该变量进行比较,如果为false,则执行代码,如果为true,则不执行。仅此代码是不够的!这段代码不应该循环,它必须是代码中重复调用它的其他东西。设置全局变量,如果底部被击中,则在其中存储值。每次加载新数据时,都将该变量设置为false。在if语句中进行比较