Sawtaytoes
3/20/2019 - 8:13 AM

Vanilla JS Example

const queue1 = []
const queue2 = []

let timeoutIds = {}
let queue = queue1

const clearQueue = (
  queue,
) => {
  queue
  .splice(
    0,
    queue.length,
  )
}

const renderNode = (
  id,
) => (
  setTimeout(
    () => {
      queue
      .push(id)
    },
    getRandomTimeout(),
  )
)

let intervalId

const createInterval = () => (
  setInterval(
    () => {
      queue = (
        queue1.length > 0
        ? queue2
        : queue1
      )

      const tempQueue = (
        queue1.length > 0
        ? queue1
        : queue2
      )

      for (const id of tempQueue) {
        const node = (
          document
          .getElementById(`node-${id}`)
        )

        if (!node) {
          continue
        }

        node.innerHTML = getRandomValue()
        node.style.color = getRandomColor()

        timeoutIds[id] = renderNode(id)
      }

      clearQueue(tempQueue)
    },
    40,
  )
)