我收到错误消息
未捕获SyntaxError:非法的break语句
在break;
上,如果服务器返回5,我想停止for
循环。我使用以下代码:
<script type="text/javascript">
function getrblstatus(rbl, hash) {
var rblhosts = [<?php echo $rblhosts ?>];
var ActiveRbls = rblhosts.length;
var progress = '1';
for (i = 0; i < ActiveRbls; i++) {
///// Post data to server /////
$.ajax({
url: '<?php echo $site_url?>/includes/lib/ajax.php',
data: {
action: 'getrblstatus',
for: rbl,
hash: hash,
rbl_host: rblhosts[i]
},
type: 'post',
success: function (data) {
var percent = (progress/ActiveRbls)*100;
percent = percent.toFixed(2);
if(data == 5) {
console.log('Hash not correct');
break;
}
else{
$('#rbl_table > tbody').append(data);
$('#progressbar').width(percent+'%');
$("#progressbar").html(percent+"% Completed");
progress ++;
continue;
}
}
});
}
}
</script>
发布于 2017-12-26 18:00:33
不能将异步代码( AJAX调用)与同步代码(for/break/continue)混合使用。因此,您可以对AJAX请求执行await
:
async function getrblstatus(rbl, hash) {
var hosts = [<?php echo $rblhosts ?>], progress = 1;
for (const rbl_host of hosts) {
const data = await new Promise((success, error) => $.ajax({
url: '<?php echo $site_url?>/includes/lib/ajax.php',
data: {
action: 'getrblstatus',
for: rbl,
hash: hash,
rbl_host
},
type: 'post', success
}));
var percent = (progress++ / hosts.length) * 100;
if(data == 5) {
console.log('Hash not correct');
break;
}
else {
$('#rbl_table > tbody').append(data);
$('#progressbar').width(percent+'%');
$("#progressbar").html(percent+"% Completed");
}
}
}
由于await
,break;
处于for
循环级别。
https://stackoverflow.com/questions/47976384
复制相似问题