简体   繁体   English

jQuery .empty()导致AJAX填充的div闪烁

[英]jQuery .empty() causes AJAX populated div to blink

I am writing a dynamic website that uses AJAX to check the status of inventory items between front-end and back-end employees of a firm. 我正在编写一个动态网站,该网站使用AJAX来检查公司的前端和后端员工之间的库存项目状态。 One employee submits a request and the people in the back see it pop up on a queue and then respond if the requested item is or isn't in stock. 一名员工提交了一个请求,后面的人看到该请求突然出现在队列中,然后响应所请求的物品是否有库存。 The front-end employee then sees the status of their request change appropriately. 然后,前端员工会看到他们的请求状态已适当更改。

The problem I am having, and perhaps there is a better way of doing things, occurs when the code uses AJAX to retrieve all active requests from a table in a database and then uses jQuery 's .empty() function to clear out the container div which holds all of the old requests(setInterval() every 2 seconds) and regenerates the queue using a PHP script which builds a on the fly based on the AJAX results. 当代码使用AJAX从数据库中的表中检索所有活动请求,然后使用jQuery的.empty()函数清除容器时,便出现了我可能遇到的问题,并且也许还有更好的处理方法。 div保留所有旧请求(每2秒setInterval()),并使用PHP脚本重新生成队列,该脚本基于AJAX结果动态构建。

The setInterval() function which calls the function that .empty() the container div causes queue to visibly disappear and reappear. setInterval()函数调用容器div的.empty()函数使队列明显消失并重新出现。 Is there a better way of updating a queue using jQuery rather than emptying out the entire queue and then rebuilding it based on updated statuses? 有没有更好的方法来使用jQuery更新队列,而不是清空整个队列,然后根据更新后的状态重建队列? I cannot simply query for the latest request because the statuses of older requests may have changed and need to be updated as well. 我不能简单地查询最新请求,因为旧请求的状态可能已更改,并且也需要更新。

Hopefully this is clear, if not then please ask what more information is need, and any help will be greatly appreciated, thanks! 希望这很清楚,如果不是这样,请询问需要更多的信息,我们将不胜感激,谢谢!

Something like this... 像这样

$(document).ready(function(){Update();});

function Update() {
    $.ajax({
        url: 'http://example.com/url/to/poll',
        success: function(data) {
                $('#MyDiv').html(data);
                setTimeout('Update()', 2000);
        },
        fail: function() {
            //Something went wrong. Inform user/try again as appropriate
            alert('Failed');
            setTimeout('Update()', 2000);
        }
    })
}

It only starts the next 2s delay after the current one completes. 当前延迟完成后,它才开始下一个2s延迟。 Also, it never actually empties the Div, it just replaces the old contents with the new. 而且,它实际上不会清空Div,只是将旧内容替换为新内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM