1

我现在正在使用这段代码,效果很好,除了它向网络服务器发出三个请求:

var refreshspeed=1000

        function moreSnow() {
    $("#uptimedynamic").load("index.html #uptimedynamic");
    $("#cpuloaddynamic").load("index.html #cpuloaddynamic");
    $("#meminfodynamic").load("index.html #meminfodynamic");
    setTimeout("moreSnow()", refreshspeed);
    }

有人可以告诉我如何让它做同样的事情,但只读取一次 index.html 吗?它需要保持相同的重复循环设置:)

4

1 回答 1

0

这应该这样做

/* cache selectors in main page to avoid searching for them every second*/
var $upTime=$("#uptimedynamic"),  $cpuload = $("#cpuloaddynamic"), $meminfo=$("#meminfodynamic")


function moreSnow() {
    $.get("index.html", function(data){
        /* create a jQuery object from the retrieved page html that can then be traversed*/
        var $data=$(data);

        $upTime.html( $data.find('#uptimedynamic').html() );
        $cpuload.html( $data.find("#cpuloaddynamic").html() );
        $meminfo.html( $data.find("#meminfodynamic").html() );

    });
}

通过仅检索每个元素的内容,您还可以避免在页面中重复 ID 和潜在的样式问题

于 2012-12-25T07:15:23.223 回答