0

I have a script that creates a progress bar and is style with CSS. It works great, but once the bar reaches the end, it stops and I can not get the script to loop so that it starts again. How can I loop this script so that the progress bar starts over once it reaches the end?

<script type="text/javascript">
$(document).ready(function() {
var el = $(''#progress'');
el.animate({
width: "100%"
}, 40000);
});
</script>


<style>
#progressKeeper {background:#f2f2f2;display:none;width: 400px;height: 18px;border: 1px         solid #CCC;-moz-border-radius:7px; border-radius:7px;color:#f2f2f2;font-family:Arial;font-    weight:bold;font-style:italic;font-size:.9em;margin-bottom:2000px;}
#progress {background: #005c9e;width: 0;height: 17px;-moz-border-radius:7px; border-    radius:7px;}
</style>
1
  • One thing that I would try is to wrap the script in a function, and then loop with a certain condition like: for (var i=0; i < progressCount; i++) { showProgressBar(); } You may want to wrap the whole thing in other function... Commented Sep 25, 2013 at 14:12

2 Answers 2

3

Drop the jQuery, use CSS3!

#progress {
    animation:progress 40s linear infinite;
    -webkit-animation:progress 40s linear infinite;
}

@keyframes progress {from {width:0} to {width:100%}}
@-webkit-keyframes progress {from {width:0} to {width:100%}}

If you must support outdated browsers... well, pass a callback to the animate function to tell it to start the animation again. Something like this:

$(function() {
    var prog = $("#progress");
    anim();
    function anim() {
        prog.css({width:0});
        prog.animate({width:"100%"},40000,anim);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

2

Make it a function and pass that function to your .animate call as completion handler. See jQuery().animate().

Something like:

$(document).ready(function() {
    function animateProgressBar( ) {
        $('#progress').width(0).animate({
            width : "100%"
        }, 40000, animateProgressBar);
    }

    animateProgressBar();
});

(Untested)

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.