From 89b8a4c9fc5c9ad18b519f8c940acdbace02724b Mon Sep 17 00:00:00 2001 From: Martin Ahrens Date: Thu, 17 Oct 2013 16:40:35 -0400 Subject: [PATCH 1/3] fix attempt for bar < 100 pixels This change addresses the issue where a progress bar of less than 100 pixels will show incorrect text in some situations. I understood the problem to be that for bars with fewer than 100 pixels comparing ratios between bar pixels to the percentage of the represented fraction will not always be equal. For example the ratio 1 / 13 as shown on a bar of 45 pixels: 1/13 = 0.076... ~= 8% 45 * (1/13) = 3.4... ~= 3 pixels 3 pixels / 45 pixels = 0.066... ~= 7% The code was expecting the 7% to equal 8% before terminating. Because this condition never happens the interval function keeps running and overwrites the progress bar text even after future changes! At first I thought different rounding would resolve it (round the 7.6 down to 7 and the 6.6 up to 7). However I think that won't work in every case. I now prefer the above solution also because it's easier to understand. The interval function is now named `intervalUpdate` and it stores the previous percentage of pixel transition progress in `intervalUpdate.prev_percentage` and compares this to `current_percentage`. If these two values are equal it is assumed that no more progress is being made and so the interval will terminate. --- bootstrap-progressbar.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bootstrap-progressbar.js b/bootstrap-progressbar.js index 50907cf..2353f23 100644 --- a/bootstrap-progressbar.js +++ b/bootstrap-progressbar.js @@ -93,7 +93,7 @@ $this.css('width', percentage + '%'); } - var progress = setInterval(function() { + var progress = setInterval(function intervalUpdate() { if (is_vertical) { this_size = $this.height(); parent_size = $parent.height(); @@ -106,12 +106,13 @@ current_percentage = Math.round(100 * this_size / parent_size); current_value = Math.round(this_size / parent_size * (aria_valuemax - aria_valuemin)); - if (current_percentage >= percentage) { + if (current_percentage >= percentage || intervalUpdate.prev_percentage === current_percentage) { current_percentage = percentage; current_value = aria_valuetransitiongoal; done(); clearInterval(progress); } + intervalUpdate.prev_percentage = current_percentage; if (options.display_text !== 'none') { text = options.use_percentage ? options.percent_format(current_percentage) : options.amount_format(current_value, aria_valuemax); From bc86de3da2fc2858569eafb5444763f21d5cb74f Mon Sep 17 00:00:00 2001 From: Martin Ahrens Date: Thu, 17 Oct 2013 17:21:04 -0400 Subject: [PATCH 2/3] allow smooth transitions on the way down. terminating a transition when the `current_percentage` is greater than `percentage` will make the text changing end prematurely. --- bootstrap-progressbar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap-progressbar.js b/bootstrap-progressbar.js index 2353f23..ae507a0 100644 --- a/bootstrap-progressbar.js +++ b/bootstrap-progressbar.js @@ -106,7 +106,7 @@ current_percentage = Math.round(100 * this_size / parent_size); current_value = Math.round(this_size / parent_size * (aria_valuemax - aria_valuemin)); - if (current_percentage >= percentage || intervalUpdate.prev_percentage === current_percentage) { + if (current_percentage === percentage || intervalUpdate.prev_percentage === current_percentage) { current_percentage = percentage; current_value = aria_valuetransitiongoal; done(); From 14108e4fad7125af0f5addb3f0841ca5fa526299 Mon Sep 17 00:00:00 2001 From: Martin Ahrens Date: Thu, 17 Oct 2013 17:29:55 -0400 Subject: [PATCH 3/3] no need to check current_percentage equals percentage at all. when the progress bar transition comes to rest that's when we're done. no need for superfluous check. --- bootstrap-progressbar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bootstrap-progressbar.js b/bootstrap-progressbar.js index ae507a0..a988527 100644 --- a/bootstrap-progressbar.js +++ b/bootstrap-progressbar.js @@ -106,7 +106,7 @@ current_percentage = Math.round(100 * this_size / parent_size); current_value = Math.round(this_size / parent_size * (aria_valuemax - aria_valuemin)); - if (current_percentage === percentage || intervalUpdate.prev_percentage === current_percentage) { + if (intervalUpdate.prev_percentage === current_percentage) { current_percentage = percentage; current_value = aria_valuetransitiongoal; done();