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.

    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集