Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6782155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:42:28+00:00 2026-05-26T16:42:28+00:00

What I want is this (news ticker type functionality): Get a list of li’s

  • 0

What I want is this (news ticker type functionality):

  1. Get a list of li’s from a ul tag
  2. Loop through all the li’s and get the text
  3. display the text in the console via firefox console.log()
  4. get the next li and repeat till all li’s have been displayed

That’s the goal, but setTimeout is not running as I thought it would. Only the LAST iteration (“Post Four”) is showing. And that (“Post Four”) is showing four times in a row.

<body>
<ul id="post_list">
 <li>Post One</li>
 <li>Post Two</li>
 <li>Post Three</li>
 <li>Post Four</li>
</ul>

<script type="text/javascript">
var ul = document.getElementById('post_list');
var li = ul.getElementsByTagName('li');

for(var x=0; x < li.length; x++){
    var li_text = li[x].childNodes[0].nodeValue;
    setTimeout(function(){showText(li_text)}, 1000);
}

function showText(text) {
    console.log(text);
}           
</script>
</body>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:42:29+00:00Added an answer on May 26, 2026 at 4:42 pm

    The reason this is happening is because of closures. The for loop block has closures around it, so when you reference ‘li_text’ it was always equal the last value that li_text was set to. The for loop does not create a separate closure for each iteration through the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want this text all on one line, not spaced on two sepearate lines.
Trying to get just a text description out of this good news feed, i
This is strange. In the news details page, I want to take a few
I want this for some conditional compilation code that will run in all IE's
I want to parse Google News RSS with PHP, to get actual links of
function removeNewsItem(id,idStamp,obj){ $('#whiteCover')show(); $('#contentOfBox').html= ('Are you sure you want ot completely remove this news
I want to make an MVC route for a list of news, which can
I want to read this file:- http://www.somehost.com/products/, A0,D1,L0,T0 http://www.somehost.com/news/rel, A1,D0,L1,T0 http://istor.somehost.com, A0, D1, L0,
I want to serialize like this <urlset xmlns=http://www.sitemaps.org/schemas/sitemap/0.9 xmlns:news=http://www.google.com/schemas/sitemap-news/0.9> </urlset> But wrong result generated.
I want to get some data from an external page than display it with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.