簡體   English   中英

如何停止JavaScript中的if-else語句循環

[英]How to stop if-else statement loop in JavaScript

我有這個代碼

// 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);

頭頂掉了兩條路

  1. 檢查光標在哪里,如果它離底部一定距離,則不要觸發該代碼
  2. 將標志存儲在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;

}

請以此為起點,而不是復制粘貼解決方案。

您可以使用全局變量來確定是否必須顯示消息

像這樣的東西:

var isDisplayed = false;

然后

if (isDisplayed == false)
{
   isDisplayed = true;
   displayMessage();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM