I am trying to create a flight widget

Rahan Ajai
let mainid = document.getElementById("main_id");

function createcards(txt) {
  let cards = document.createElement("div");
  cards.classList.add("ex-1");
  cards.textContent = txt;
  return cards;
}

function addElements(parent, children) {

    children.forEach((e) => {
      setTimeout(() => {
        parent.appendChild(e); 
      }, 1000);
 
    });
  
}

items = [
  createcards(1),
  createcards(2),
  createcards(3),
  createcards(4),
  createcards(5),
];

addElements(mainid, items);

I want to append each child one by one. I meant by changing the speed of setTimeout. I tried doing it using for loop but it's not working as expected. please anyone help me?

vicnoob

Seem you a bit misunderstanding about the eventloop and how setTimeout work. Your code will delay 5 function, each 1000ms and then run those at the same time. For more you can check out this link

I tried with setInterval instead, you can check it out to see if it fit your problem

let mainid = document.getElementById("main_id");

function createcards(txt) {
  let cards = document.createElement("div");
  cards.classList.add("ex-1");
  cards.textContent = txt;
  return cards;
}

function addElements(parent, children) {
  const interval = setInterval(item => {
    if (!children.length) {
      clearInterval(interval)
      return
    }
    parent.appendChild(children[0])
    children.shift()
  }, 1000)
}


items = [
  createcards(1),
  createcards(2),
  createcards(3),
  createcards(4),
  createcards(5),
];

addElements(mainid, items);
<div id="main_id" />

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

I am trying to create widget in next.js app

I am new to python and i am trying to create a leaderboard

I am trying to create a box in React, and it is not working

I am trying to create a dynamic spinner

I am trying to create a seven segment scoreboard

I am trying to create a Clamp method in java

I am trying to create a table with sqlite in Kotlin

I am trying to create a subscription but an error occurs

I am trying to trigger Alt tab Widget with SPACE + A

I am trying to wrap my Column widget in a SingleChildScrollView but not able to

I have an appointment scheduling application I am trying to create

I am trying to create a table (Order) using schema with mutable FK

I am trying to create signup pages in react website but it's not working

Error when i am trying to create a zip from 2 files

I am trying to create a function called codechecker to determine a code is correct or not?

Why am I getting a _TypeError when trying to create a list?

As you can see I am trying to create a link elements?

There is no regions when I am trying to create a NAT Gateway GCP

I am Trying to create a webview activity but the whole website is not loaded

I am trying to create a table with only two foreign key

I am trying to create a program that converts bytes to human readable JAVA

I am trying to create a DocumentTermMatrix while keeping all special characters

Why I am getting an error when trying to create an audience?

I am trying to create a new Cart schema with a request

I am trying to create the below header with the help of XWPFDocument and merging cells

Hi! I am trying to create a button that will change the css grid layout

Why am I getting a syntax error when trying to create a table?

I am trying to create a Login website by Connecting Database to Registration Page

Why is there spacing in-between these columns that I am trying to create?