[英]How to stop progress bar at 100% in ajax request?
我要在执行ajax
请求时使用引导程序制作progress bar
。 我可以启动进度条,但是问题是我不知道让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%?
成功时必须调用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 + "%");
}
});
$(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>
$(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); } } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.