I have a certain function in mind:
function fetchDesign (items) {
items.map(item => item.classList.add('selected'))
// additional code here
}
Is there a way to trigger item.classList.add('selected')
every 2 seconds, while ensuring that the rest of the code runs synchronously after all iterations are completed?
(Just to clarify, 'items' is an array containing HTML elements)
UPDATE: Here's what I've attempted so far:
items.forEach(item => setTimeout(() => { item.classList.add('selected') }, 2000))
items.map(item => setTimeout(() => { item.classList.add('selected') }, 2000))
setTimeout(() => {
items.map(item => item.classList.add('selected'))
}, 2000)
setInterval(() => {
items.map(item => item.classList.add('selected'))
}, 2000)
My current struggle includes encountering the exact issues I set out not to face initially:
addClass
does not get delayed as required and all iterations happen simultaneously. Furthermore, the following lines of code run asynchronously, causing immediate display of console.log
outputs.