我正在嘗試使用頁面上的元素作為進度指示器(以百分比為單位)。但是,當我innerText在for回圈內部呼叫時,Chrome 會凍結并且不顯示進度。這是示例代碼(只是為了展示問題):
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<meta charset="utf-8"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1 id="a">Hello!</h1>
</body>
<script type="text/javascript">
function initTrs(){
for(var i = 1; i<=100; i ){
document.getElementById("a").innerText = i;
$.ajax({
url: "https://www.google.com",
async: false,
done: function (a) {}
});
}
}
window.addEventListener("load", () => initTrs());
</script>
</html>
Firefox 可以正常使用;但是,Chrome 會凍結,直到for回圈完全完成。
有人可以幫我解決這個問題嗎?謝謝!
uj5u.com熱心網友回復:
最后我是這樣解決的:
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
<meta charset="utf-8"/>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h1 id="a">Hello!</h1>
</body>
<script type="text/javascript">
function initTrs(){
for(var i = 1; i<=100; i ){
const iter = i;
setTimeout(() => {
document.getElementById("a").innerText = iter;
$.ajax({
url: "https://www.google.com",
async: false,
done: function (a) {}
});
});
}
}
window.addEventListener("load", () => initTrs());
</script>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/377136.html
標籤:javascript html 阿贾克斯
上一篇:ajax回傳未定義的json資料
