我想在执行progress bar
请求时使用引导程序创建ajax
。我可以启动进度栏,但是问题是,我不知道有一种方法可以使ajax call
为success
时将其停止在100%。以下是我编写的代码:
html
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
</div>
jQuery
$(document).ready(function(){
let percentValue = 0,
progressBar = $('.progress-bar');
timer = setInterval(startBar, 500);
$.ajax({
url: url,
type: "GET",
dataType: "json",
beforeSend: function(){
startBar();
},
success: function(){
clearInterval(timer);
}
}).done(function(data){
alert("success");
console.log(data);
}).fail(function(jqxhr, textStatus, error ){
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
function startBar(){
percentValue += 25;
progressBar.css("width", percentValue + "%").html(percentValue + "%");
}
});
我收到的结果大部分是在75%处停止。我没有上传任何照片。它只是从服务器获取数据。如何使其停止在100%?
答案 0 :(得分:1)
成功后,您必须调用startBar()
函数:
$(document).ready(function(){
let percentValue = 0,
progressBar = $('.progress-bar');
timer = setInterval(startBar, 500);
$.ajax({
url: url,
type: "GET",
dataType: "json",
beforeSend: function(){
startBar();
},
success: function(){
clearInterval(timer);
}
}).done(function(data){
alert("success");
startBar();
console.log(data);
}).fail(function(jqxhr, textStatus, error ){
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
function startBar(){
percentValue += 25;
progressBar.css("width", percentValue + "%").html(percentValue + "%");
}
});
答案 1 :(得分:1)
$(document).ready(function() {
let percentValue = 0,
progressBar = $('.progress-bar');
timer = setInterval(startBar, 500);
$.ajax({
url: "http://www.mocky.io/v2/5c3f66243500002d2cec3954",
type: "GET",
dataType: "json",
beforeSend: function() {
startBar();
},
success: function() {
clearInterval(timer);
}
}).done(function(data) {
alert("success");
console.log(data);
}).fail(function(jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
function startBar() {
if (percentValue < 100) {
percentValue += 25;
}
progressBar.css("width", percentValue + "%").html(percentValue + "%");
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="progress">
<div class="progress-bar" role="progressbar"></div>
</div>
答案 2 :(得分:1)
$(document).ready(function(){
let percentValue = 0,
progressBar = $('.progress-bar');
timer = setInterval(startBar, 500);
$.ajax({
url: url,
type: "GET",
dataType: "json",
success: function(){
if (percentValue < 100) percentValue = 75;
}
}).done(function(data){
alert("success");
console.log(data);
}).fail(function(jqxhr, textStatus, error ){
var err = textStatus + ", " + error;
console.log( "Request Failed: " + err );
});
function startBar(){
if (percentValue < 100) {
percentValue += 25;
progressBar.css("width", percentValue + "%").html(percentValue + "%");
} else {
clearInterval(timer);
}
}
});