Reputation: 87
Here is the code, below the console.log part is my issue.
It logs out all 3 classes of offset in a tag like this :
<tag id="id1" class="$offset1, $offset2, $offset3">
But I just need 1 class of offset in a tag like this:
<tag id="id1" class="$offset1">
How do I fix this issue?
Explanation:
add new classes to html element
function $addClsls(element, ...newClass) {
return element.classList.add(...newClass);
}
remove classes from html element
function $rmClsls(element, ...rmClass) {
return element.classList.remove(...rmClass);
}
select an element id or class
function $select(element) {
return document.querySelector(element);
}
a function to add or remove class from a selected element
function $switch(selectEle, eleClass, indexNum) {
switch (indexNum) {
case 0:
$addClsls(selectEle, eleClass);
break;
case 1:
$rmClsls(selectEle, eleClass);
break;
default:
}
}
get 3 element ids into array
let groupElements = [$select('#id-1'), $select('#id-2'), $select('#id-
3')];
get 3 element classes into array(no need to put . )
let groupOffsets = ['offset1', 'offset2', 'offset3'];
set init time value
let addTime = 0;
check if true to map through groupElements and groupOffsets and set delay time
if (true) {
groupElements.map( i => {
setTimeout( () => {
$switch(i, groupOffsets.map( x => x ), 0)
console.log(i) // <= logs out results
}, 0 + addTime );
addTime += 1000;
});
}
the GOAL is to log out like this :
<tag id="id-1" class="offset1"> </tag>
<tag id="id-2" class="offset2"> </tag>
<tag id="id-3" class="offset3"> </tag>
but the issue is like this :
<tag id="id-1" class="offset1,offset2,offset3"></tag>
<tag id="id-2" class="offset1,offset2,offset3"></tag>
<tag id="id-3" class="offset1,offset2,offset3"></tag>
Upvotes: 0
Views: 1185
Reputation: 87
Well, I got it fixed. So I'm gonna answer my own question. ;)
I tried another way to do map function, I used a 2-dimensional array instead of 2 separated arrays
let groupItems = [
[$select('#id-1'), 'offset1'],
[$select('#id-2'), 'offset2'],
[$select('#id-3'), 'offset3']
]
groupitems.map( items => {
setTimeout( () => {
// destruct first dimensional
let [ x ] = [items];
// destruct second dimensional
let [ i, j ] = x;
// first round i = $select('#id-1'); j = 'offset2';
// second and third round so forth
sectionSwitch( i, j, 1);
}, 0 + addTime);
addTime += 1000;
})
Upvotes: 1