I want to create a progress bar that has a duration ( the time that it takes to finish the animation) and a percentage.
So I want this progress bar to take 3000ms to finish ( to get to 100%):
So far :
<div id="box"></div>
<script>
function start(){
var duration = 5000; // it should finish in 5 seconds !
var max = 100;
var i = 0 ;
var interval = setInterval(function(){
i++;
offset = (max*i)/duration;
console.log(offset);
$("#box").css("width", offset + "px");
$("#box").text(parseInt(offset) + "%");
if(i>=duration){
alert("done "+i);
clearInterval(interval);
}
}, 1);
}
</script>
It works but it takes way longer that 5000ms .
I've also added Jquery tag because I don't care if I do this with javascript or jquery
Thanks guys.
Feel free to tweak the below as needed, but the main problems are fixed. Namely, your interval shouldn't be running every 1 millisecond, and it should complete at 100%. The below will set your interval to always run at each 1%.
function start(){
var duration = 5000; // it should finish in 5 seconds !
var percent = duration / 100; // 1 percent of duration
var i = 0 ;
var interval = setInterval(function(){
i++;
$("#box").css("width", i + "px");
$("#box").text(i + "%");
if(i>=100){
alert("done");
clearInterval(interval);
}
}, percent);
}
The simplest solution could be is ot use jQuery's .animate()
function start() { var duration = 5000; // it should finish in 5 seconds ! $("#box").stop().css("width", 0).animate({ width: 100 }, { duration: duration, progress: function(promise, progress, ms) { $(this).text(Math.round(progress * 100) + '%'); } }); } start()
#box { border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="box"></div>
another solution will be is to look at the time difference
function start() { var duration = 5000; // it should finish in 5 seconds ! var st = new Date().getTime(); var interval = setInterval(function() { var diff = Math.round(new Date().getTime() - st), val = Math.round(diff / duration * 100); val = val > 100 ? 100 : val; $("#box").css("width", val + "px"); $("#box").text(val + "%"); if (diff >= duration) { clearInterval(interval); } }, 100); } start()
#box { border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="box"></div>
Same using requestAnimationFrame
function start() { var duration = 5000; // it should finish in 5 seconds ! var st = window.performance.now(); window.requestAnimationFrame(function step(time) { var diff = Math.round(time - st), val = Math.round(diff / duration * 100); val = val > 100 ? 100 : val; $("#box").css("width", val + "px"); $("#box").text(val + "%"); if (diff < duration) { window.requestAnimationFrame(step); } }) } start()
#box { border: 1px solid red; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="box"></div>
Not sure if you're using it yet, but you could bootstrap to do this.
<div class="progress-bar" style="width: 0;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" >
var value = 0;
function start(){
value += 5;
$( ".progress-bar" ).css("width", value+"%").attr("aria-valuenow", value);
if ( value%5 == 0 ) {
return setTimeout(restart, 100);
}
if(value >= 100)
return;
else
setTimeout(restart, 50);
}
function restart() {
start();
}
I used the answer provided by some of you but you got one thing wrong on the progress bar. You need to change the "px" in jquery to be "%" otherwise the progress bar will only be 100px wide. Since I am using the Bootstrap Progress bar the values here amend to what is already there and fill up the progress wrapper as the page loads.
function start() {
var duration = 5000; // it should finish in 5 seconds !
var st = window.performance.now();
window.requestAnimationFrame(function step(time) {
var diff = Math.round(time - st),
val = Math.round(diff / duration * 100);
val = val > 100 ? 100 : val;
$(".progress-bar").css("width", val + "%");
$(".progress-bar").text(val + "%");
if (diff < duration) {
window.requestAnimationFrame(step);
}
})
}
start()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.