javascript - 使用 jQuery 和 AJAX 轮询服务器,没有初始延迟

标签 javascript jquery ajax polling

问题

大家好,我正在尝试以 10 秒的设定间隔刷新表数据...问题是,当我最初加载页面时,在显示表之前有 10 秒的延迟...

代码

标记

<h2>Employee List</h2>
<!-- table -->
<table class="table table-striped table-hover table-responsive">
    <thead>
        <tr>
            <td>ID</td>
            <td>Emplyee Name</td>
            <td>Address</td>
            <td>Created at</td>
            <td>Action</td>
        </tr>
    </thead>

    <!-- table content dynamically generated by the javascript -->
    <tbody id="showdata"></tbody>
    <!-- end content -->

</table>
<!-- end table -->

jQuery

$(function(){


        pollServer();

        function pollServer(){

            window.setTimeout(function () {
                alert('hey');
                $.ajax({ // this is a json object inside the function
                    type: 'ajax',
                    url: '<?php echo base_url('employee/showAllEmployee'); ?>',
                    async: false,
                    dataType: 'json',
                    success: function(data){
                        console.log(data);
                        var html = '';
                        var i;
                        for(i = 0; i < data.length; i++){
                            html += '<tr>' +
                                        '<td>' + data[i].employee_id + '</td>' +
                                        '<td>' + data[i].employee_name + '</td>' +
                                        '<td>' + data[i].address + '</td>' +
                                        '<td>' + data[i].created_at + '</td>' +
                                        '<td>' +
                                            '<div class="pull-right">' +
                                                '<a class="btn btn-info btn-xs item-edit" href="javascript:;" data="' + data[i].employee_id + '">Edit</a>' +
                                                '<a class="btn btn-danger btn-xs item-delete" href="javascript:;" data="' + data[i].employee_id + '"><i class="fa fa-times"></i></a>' +
                                            '</div>' +
                                        '</td>' +
                                    '</tr>'
                        }
                        $('#showdata').html(html);
                    },
                    error: function(){
                        alert('Could not get Data from Database');

                    },
                    complete: function(){
                        pollServer();
                    }
                })
            }, 10000);
        }
    });

问题

如何在页面加载时获取数据,然后每 10 秒 ping 一次服务器?

最佳答案

更改 pollServer 如下

function pollServer(){
    $.ajax({ // this is a json object inside the function
    // removed for clarity
        complete: function(){
            setTimeout(pollServer, 10000);
        }
    });
}

关于javascript - 使用 jQuery 和 AJAX 轮询服务器,没有初始延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41869946/

相关文章:

JavaScript 在 Brackets 代码编辑器中很奇怪

javascript - 无法使用 NodeJS 登录生成 jwt token

javascript - 如何在单击按钮时获取最接近类的 td 值?

ajax - 来自 ajax post 的数据将 null 传递给 Controller

javascript - 每次 AJAX 调用时使用 JQuery SlideUp、Delay 和 SlideDown

javascript - 如何将输入文件参数从html页面传递给Javascript

javascript - react : pass DOM element properties to child

ajax - 从 JQuery 向 Web API 进行 POST 时出现问题

php - 优化 JavaScript CSS 下载

javascript - 测量ajax接收时间