dongshi3361 2010-11-16 11:43
浏览 141

使用Jquery自动刷新多个div

Im creating a dashboard for a large display at work, I have managed to get one div auto refreshing a php request page, but I can't think of a way of using this function on another div for another script without copying the entire function and renaming it, which just seems dumb.

This is what I have so far:

<script type="text/JavaScript"> 
$(document).ready(
function update() 
{
    $.get("response.php", function(data){
            $("#ajaxtest").html(data);
            window.setTimeout(update, 10000);
        });
});
</script>

I did so some searching on stackoverflow and found this nifty little extract, although its for single div use only... (second example: auto refreshing div with jquery)

  • 写回答

5条回答 默认 最新

  • doudouxuqh198138 2010-11-16 11:50
    关注

    You've out your update function as to be called directly when the document is ready. That's not necessarily a good idea, you want that function to be callable multiple times. For example, consider the following (untested code!):

    function update(divId) 
    {
       $.get("response.php", function(data){
       $(divId).html(data);
       window.setTimeout(update, 10000);
    }
    

    and then

    $(document).ready(function(){
       update("#ajaxtest");
       update("#otherdiv");
       update(".divsbycssclass");
    });
    

    If you need a different url pass that in too. You can call this update method by timer or event:

    $("a").click(function(){
       update("#ajaxtest");
    });
    

    Is that what you were referring to? If not, feel free to comment and I'll edit.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度