1

i am making an animated progress bar. It's code is quite simple but the problem is that I can't figure out how to avoid repeating some jQuery code, you can see it in this Fiddle:

http://jsfiddle.net/N6c2q/

basically, this is what repeats:

$('.bar-value').animate({width: '+=1%'},50,function(){
    var percent = $('.bar-value').width() / $('.bar-value').parent().width()*100;
    var percInt = parseInt(percent);
    $('.progress').text(percInt+"%");
});

that just increases the width of the inner div (progress) by 1%, and increases .progress span number by 1. Is there any way to do what i'm trying to do without repeating that code?

Thanks in advance!

4
  • 2
    Have you looked into the jQuery UI progressbar? Commented May 5, 2014 at 0:57
  • 2
    You can just wrap that bit in a for loop. Commented May 5, 2014 at 0:57
  • @Luxelin haha yess but i like doing things by myself Commented May 5, 2014 at 0:59
  • See Darius's response below :) Commented May 5, 2014 at 1:02

3 Answers 3

6

Just give duration and amount you'd like to animate:

$('.bar-value').animate({width: '+=100%'},2000,function(){
    var percent = $('.bar-value').width() / $('.bar-value').parent().width()*100;
    var percInt = parseInt(percent);
    $('.progress').text(percInt+"%");
});
Sign up to request clarification or add additional context in comments.

10 Comments

I really doubt that this is correct. If there's a progress for every bar-value, then this isn't right at all. He's already looping when he calls .animate().
True statement cookie monster, I didn't look at that close enough! No need to loop at all. Just animate from 0-100% and set an appropriate duration.
hey , it worked, but, how can i ejecute a function after the loop finished?
@DariusHoule: I think you're right now. I couldn't tell what he meant by repeating, but it seems he just doesn't know how animate works.
@user3597089: Here's a fixed up version that builds on the changes Darius made. jsfiddle.net/N6c2q/4
|
1

Use recursion:

function showProgress(finish) {
    $('.bar-value').animate({
        width: '+=1%'
    }, 50, function () {
        var percent = $('.bar-value').width() / $('.bar-value').parent().width() * 100;
        var percInt = parseInt(percent);
        $('.progress').text(percInt + "%");

        if (percInt != 100) {
            showProgress(finish);
        } else {
            if (finish) finish();
        }
    });
}

Fiddle

Edit: fix

5 Comments

Why use recursion when you can do it more elegantly using iteration?
Recursion is more elegant to me.
Until you run out of memory. Iteration is almost always better if you can use it.
If you're going to talk about elegance, format your code correctly.
You are welcome. Note that you can re-indent messy code using jsbeautifier.org or jsfiddle.net, and format it in an answer or question by selecting your code and then clicking in the {} icon in the formatting bar.
0

Of course, that code is madness!!

So I would recommend you to use jQuery UI progress bar

Otherwise you can use a loop to do what you are trying to achieve:

http://jsfiddle.net/db306/N6c2q/3/

$(document).ready(function(){
    var percent = $('.bar-value').width() / $('.bar-value').parent().width()*100;
    $('.progress').text(percent+"%");

    $('button').click(function(){
        $('.loader').fadeIn(1200);
        $('button').attr('disabled','disabled');


        for (var i=0; i<100;i++){
            $('.bar-value').animate({width: '+=1%'},50,function(){
            var percent = $('.bar-value').width() / $('.bar-value').parent().width()*100;
            var percInt = parseInt(percent);
            $('.progress').text(percInt+"%");
        });
        }
    });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.