i'm sorry to ask your help. I'm trying to get 3 functions in one, using array in Javascript.
Actually, I've this code to run loading bars for each language I'm trying to learn
function js() {
var elem3 = document.getElementById("barone");
var width = 0;
var id = setInterval(frame, 50);
function frame() {
if (width >= 20) {
clearInterval(id);
} else {
width++;
elem3.style.width = width + '%';
elem3.innerHTML = width * 1 + '%';
}
}
}
function php() {
var elem4 = document.getElementById("bartwo");
var width = 0;
var id = setInterval(frame, 50);
function frame() {
if (width >= 30) {
clearInterval(id);
} else {
width++;
elem4.style.width = width + '%';
elem4.innerHTML = width * 1 + '%';
}
}
}
function sql() {
var elem5 = document.getElementById("barthree");
var width = 0;
var id = setInterval(frame, 50);
function frame() {
if (width >= 30) {
clearInterval(id);
} else {
width++;
elem5.style.width = width + '%';
elem5.innerHTML = width * 1 + '%';
}
}
}
That's fine and it works well, but i'm trying to make an array for the div and the percent of loading bars
I've tried several times to shorten this code, and i'm actually stuck. This is the most (i think), closer to the answer:
var skillBar = ["barone","bartwo","barthree"];
var percentBar = [20, 30, 30];
function skill() {
for (var j = 0; j < skillBar.length; j++) {
var elem = document.getElementById(skillBar[j]);
console.log(skillBar[j]);
console.log(percentBar[j]);
var width = 0;
var id = setInterval(frame, 50);
function frame() {
console.log(percentBar[j]) <--- This return undefined
if (width >= percentBar[j]) {
clearInterval(id);
} else {
width++;
elem.style.width = width + '%';
elem.innerHTML = width * 1 + '%';
}
}
}
}
but this doesn't work. The first two console.log works fine. It returns me the values in both arrays, but the third one returns me undefined, so the loading bar never ends. Worst, it only affect the last bar. First and 2nd doesn't load.
I know it's probably obvious for most of you, but I need a little help to make things work.
P.S : Sorry for my english, it's not my native language.