weixin_33709364 2018-02-07 15:21 采纳率: 0%
浏览 29

Ajax调用以刷新网页

Update: I am using an apache server on the back-end and vanilla JS and jquery on the front-end.

Currently i have a web page that is displaying a variety of data that i am pulling from my back-end server.

How it works: I have a php script that is scraping directory names and displaying them in a dropdown. I have a refresh function set in my html for the web page that refreshes the page every 30 seconds.

The problem: I don't like the constant refresh, especially if there is nothing to update.

Is there a way to use ajax to pool my back-end server and check if new data has been entered in the directory and then update my dropdown?

Many thanks!

  • 写回答

1条回答 默认 最新

  • weixin_33724059 2018-02-07 15:25
    关注

    Use .data

    window.setInterval(function() { 
        $.getJSON('/foo', { }, function(result) { 
            //Get the old data stored in the body using jquery data
            var old_data = $('body').data('my_data');
            //If objects are not the same, update cell
            if ( ! equal_objects(result, old_data) )
                 update_cell();
            //Store the new data
            $('body').data('my_data',result);
        }); 
    }, 30000);
    

    OBS: equal_objects is a function you should implement to compare 2 objects since JavaScript doesn't provide this functionality. See this post for details: Object comparison in JavaScript

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器