开发者

Is there a way jQuery .load() can update a <div> only if the data is different?

开发者 https://www.devze.com 2023-01-07 21:56 出处:网络
Right now my code looks like this: <script typ开发者_Python百科e=\"text/javascript\"> $(document).ready(function() {

Right now my code looks like this:

<script typ开发者_Python百科e="text/javascript">
    $(document).ready(function() {
        $.ajaxSetup({
            cache: false
        });

        var loadUrl = "<?php echo site_url("admin/get_uploads"); ?>";  

        $("#result").load(loadUrl);

        var refreshId = setInterval(function() {
            $("#result").load(loadUrl);
        }, 1000);
    });
</script>

The problem is that the gets refreshed every second, therefore if I have something selected within that div, after a second, the select resets to non-selected.

Is there a way to have jQuery to only bring in new data if it's available and not refresh the every second?


$.get(loadUrl,function(result){
    if(result !=  $("#result").html())
    {
        $("#result").html(result);
    }
});

A better solution would be to use a comet type approach, long polling is fairly easy to implement.

0

精彩评论

暂无评论...
验证码 换一张
取 消