【发布时间】:2018-06-20 06:08:14
【问题描述】:
我需要显示带有多个 ajax 请求的进度条。它们不应该是异步的,因为每个请求都依赖于前一个请求。一切正常,除了进度条。这在 Chrome 中对我不起作用。在 Firefox 中它可以完美运行。执行了几次测试,我意识到当有多个没有异步的请求时,chrome 肯定不会更新 html 的任何属性。
我有这个测试代码:
$('#run').click(function(){
color_text();
});
function color_text(){
console.log("Coloring...");
sleep();
console.log('Load 1');
$('#text').html('Test 1');
sleep();
console.log('Load 2');
$('#text').html('Test 2');
sleep();
console.log('Load 3');
$('#text').html('Test 3');
}
function sleep() {
$.ajax({
url: '/echo/html/',
async: false,
data: {
html: "<p>Text echoed back to request</p>",
delay: 3
},
method: 'post',
update: 'target'
}).done( function( data ){
result = data;
$('#text').html('Test Loaded');
})
}
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<button id="run">
<h2>Run</h2>
</button>
<hr>
<div id="text"> Test</div>
</body>
</html>
在此链接中,您注意到的不仅仅是更新https://jsfiddle.net/arturoverbel/bgm4pa10/10/
【问题讨论】:
-
嵌套
onprogresss,然后在最里面添加eventObj.loadeds。 developer.mozilla.org/en-US/docs/Web/API/… -
They should not be asynchronous他们当然应该这样做,事实上大多数浏览器将来都会阻止它,除非在 webworker 中使用。 -
我明白了。但奇怪的是,这种方法在 Chrome 中无论如何都不起作用。
-
好的。在回答这个问题时,我必须异步工作。你是对的,最好使用推荐的@Keith
标签: javascript jquery html css ajax