The following 2 code snippets produce very different outcomes:
This one works as supposed: It increases the progressbar value (width) by 5 each second.
function doIncrement(increment) {
w = parseInt(document.getElementById('internal-progressbar').style.width);
document.getElementById('internal-progressbar').style.width = (w + increment) + '%';
if (parseInt(document.getElementById('internal-progressbar').style.width) < 100)
{
setTimeout(function () { doIncrement(increment) }, 1000);
}
}
This one (getelementbyid replaced with jquery selectors) produces an entire different outcome. It starts by increasing by 5 but then it increases by 34 and then by even more.
function doIncrement(increment) {
w = parseInt($('#internal-progressbar').css('width'));
$('#internal-progressbar').css('width', (w + increment) + '%');
if (parseInt(document.getElementById('internal-progressbar').style.width) < 100)
{
setTimeout(function () { doIncrement(increment) }, 1000);
}
}
What's causing this difference? I truly don't understand.
Here's a jsfiddle that demonstrates the issue: http://jsfiddle.net/k79Ph/
console.log(w)to see what's being returned?wproperly asvar w = parseInt...?.width()in the second place?.width()you know. :) api.jquery.com/width/#width-functionindex--width