How to limit my progress bar percentage to 100% only

css, html, javascript, jquery, twitter-bootstrap-3

Solution

just set to it a limit, this will work :

replace this row :

$bar.text($bar.width() / 4 + "%");

with this row :

$bar.text(Math.min($bar.width() / 4,100)+"%");

Problem

My problem is when I tried to run it and try to change tab in browser and back again the percent is more than `100%` as final result. But if i dont change tab while the progress is running it works perfect. HTML ``` <div class="container"> <div class="progress progress-striped active"> <div class="bar" style="width: 0%;"></div> </div> </div> ``` CSS ``` @import url('//netdna.bootstrapcdn.com/bootstrap/2.3.2/css/bootstrap.min.css'); .container { margin-top: 30px; width: 400px; } ``` Javascript ``` var progress = setInterval(function () { var $bar = $('.bar'); if ($bar.width() >= 400) { clearInterval(progress); $('.progress').removeClass('active'); } else { $bar.width($bar.width() + 40); } $bar.text($bar.width() / 4 + "%"); }, 800); ``` Fiddle: http://jsfiddle.net/5w5ku/1/

Original source