首页 > 解决方案 > 如何阻止 Jquery(Ajax)循环数据(包括 PHP)

问题描述

基本上,我在index.php文件中编写了一个脚本,该脚本通过调用 php 文件并从数据库中返回数据来从数据库中获取消息。然而!它循环了 2-3 次,有时还有 9 次!

有没有人能帮我查询一下!谢谢,对不起我的英语

<script>
        $(document).ready(function() {

            var flag = 0;

            $.ajax({
                type: "GET",
                url: "assets/js/ajax/messages.php",
                data: {
                    'offset': 0,
                    'limit': 6,
                    'user_id': <?php echo $User->Id(); ?>
                },
                success: function(data) {
                    $('#messages').append(data);
                    flag += 3;
                }
            });

            $(window).scroll(function() {
                if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
                    $.ajax({
                        type: "GET",
                        url: "assets/js/ajax/messages.php",
                        data: {
                            'offset': flag,
                            'limit': 3,
                            'user_id': <?php echo $User->Id(); ?>
                        },
                        success: function(data) {
                            $('#messages').append(data);
                            flag += 3;
                        }
                    });
                }
            });
        });

标签: javascriptjqueryajax

解决方案


推荐阅读