如下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);
}
我发现 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
|
1
yyfearth 2011 年 6 月 29 日
貌似是ie和非ie的onreadystatechange的支持是不同的。
我搜到的一些相关连接 http://hi.baidu.com/marsbook/blog/item/03c5398b32acfa1fc9fc7afa.html http://www.daniweb.com/web-development/javascript-dhtml-ajax/threads/285742 http://09zhi.blog.51cto.com/667284/131410 希望对你有帮助 |
|
2
yyfearth 2011 年 6 月 29 日
不好意思http://hi.baidu.com/marsbook/blog/item/03c5398b32acfa1fc9fc7afa.html这个不是你的问题,我发错了。
你看下后面2个连接吧 |
|
3
chuanjiabao OP @yyfearth 3x a lot
我尝试一下。 |
• 请不要在回答技术问题时复制粘贴 AI 生成的内容