如何为此代码添加自动刷新?

时间:2015-02-08 23:48:32

标签: javascript jquery refresh

我的名字是约翰。 (是的,我知道非常有用的信息:))我在php中创建了一个新的统计信息,我的Javascript代码看起来像

var req;

function hide() {
    for(var i=0; i<arguments.length; i++) {
        element = document.getElementById(arguments[i]);
        if (element) {
            element.style.visibility = 'hidden';
        }
    }
}

function show() {
    for(var i=0; i<arguments.length; i++) {
        element = document.getElementById(arguments[i]);
        if (element) {
            element.style.visibility = 'visible';
        }
    }
}

function loadXMLDoc(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);

        hide('lastposts');
        show('lastposts_update');

    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();

            hide('lastposts');
            show('lastposts_update');
        }
    }
}

function processReqChange() {
    if (req.readyState == 4) {
        element = document.getElementById('lastposts_text');
        if (element) {
            if (req.status == 200) {
                element.innerHTML = req.responseText;
            } else {
                element.innerHTML = "Error " + req.status + "<br />" + req.statusText;
            }
        }

        hide('lastposts_update');
        show('lastposts');
    } else {
        var status_message = new Array(
        "The query is not initialized", 
        "The query is created", 
        "The query is sent", 
        "The query is processed"
        );

        element_status = document.getElementById('status');
        if (element_status) {
            element_status.innerHTML = status_message[req.readyState];
        }
    }
}

和HTML输出

<div id="lastposts_update" style="visibility: hidden;">
    <img src="images/refresh.gif" alt="wait">&nbsp;
    <span id="status"></span>
</div>

<div id="lastposts" style="z-index: 2;">
<a href="" onclick="loadXMLDoc('{URL}'); return false;">
<img src="images/refresh.gif" alt="refresh" border="0"></a>

<span id="lastposts_text">
{IMPORT_NEW_STATISTICS}
</span>

</div>

如何修改此代码并添加自动刷新并删除手动刷新? 你能帮助我吗?我会非常感激

1 个答案:

答案 0 :(得分:0)

以下是类似的质量保证Auto refresh code in HTML using meta tags

尝试输入以下代码

<meta http-equiv="refresh" content="5">

进入HTML标头标记。