V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chuanjiabao
V2EX  ›  程序员

问个js的问题。

  •  
  •   chuanjiabao · 2011-06-29 11:37:47 +08:00 · 4910 次点击
    这是一个创建于 4888 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如下js代码,openProgressBar函数通过ajax,定时请求后端服务。
    我发现 ie7好用 chrome就不行。

    ==============================code===============================
    interval = null;

    function openProgressBar() {
    /* generate random progress-id */
    uuid = "";
    for (i = 0; i < 32; i++) {
    uuid += Math.floor(Math.random() * 16).toString(16);
    }
    /* patch the form-action tag to include the progress-id */
    document.getElementById("upload").action="/upload.php?X-Progress-ID=" + uuid;

    /* call the progress-updater every 1000ms */
    interval = window.setInterval(
    function () {
    fetch(uuid);
    },
    1000
    );
    }

    function fetch(uuid) {
    req = new XMLHttpRequest();
    req.open("GET", "/progress", 1);
    req.setRequestHeader("X-Progress-ID", uuid);
    req.onreadystatechange = function () {
    if (req.readyState == 4) {
    if (req.status == 200) {
    /* poor-man JSON parser */
    var upload = eval(req.responseText);

    document.getElementById('tp').innerHTML = upload.state;

    /* change the width if the inner progress-bar */
    if (upload.state == 'done' || upload.state == 'uploading') {
    bar = document.getElementById('progressbar');
    w = 400 * upload.received / upload.size;
    bar.style.width = w + 'px';
    }
    /* we are done, stop the interval */
    if (upload.state == 'done') {
    window.clearTimeout(interval);
    }
    }
    }
    }
    req.send(null);
    }
    3 条回复    1970-01-01 08:00:00 +08:00
    yyfearth
        1
    yyfearth  
       2011-06-29 12:02:20 +08:00
    yyfearth
        2
    yyfearth  
       2011-06-29 12:05:52 +08:00
    chuanjiabao
        3
    chuanjiabao  
    OP
       2011-06-29 13:02:26 +08:00
    @yyfearth 3x a lot
    我尝试一下。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   150 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 18ms · UTC 21:58 · PVG 05:58 · LAX 13:58 · JFK 16:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.