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 6722169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:24:56+00:00 2026-05-26T09:24:56+00:00

I am trying to write a small demo which will maintain a fixed number

  • 0

I am trying to write a small demo which will maintain a fixed number of items in a list which is being constantly updated.

This is my very first attempt at using jQuery, so I would appreciate feedback on if I can improve the code (almost surely!) and also, I am stuck on the last function truncateItems() and would appreciate some help on how to implement it.

Here is what I have written so far:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
<body>
    <div>
       <div id="container"></div>
    </div>
</body>
<script type="text/javascript">
/* <![CDATA[ */
var maxitems = 10;  // maximum number of items in container

function generateItems(){
    var min = 0;
    var max = 10;
    var items = '';

    var randnum = Math.floor(Math.random() * (max - min + 1)) + min;

    for (i=0; i < randnum; i++){
        if (i) 
            item += '\n<div class="item">Item number: ' + i + '</div>';
        else
            item = '<div class="item">Item number: ' + i + '</div>';
    }

    addItems(items);
    truncateItems(items);

    window.setTimeout(generateItems, 1000)
}

function addItems(items){
    return items + '\n' + $("#container").text(); 
}

function truncateItems(items){
    //Need to return the first N elements
    //Need to either parse items or iterate over them whilst building a new string ? ...
}


$(document).ready(function(){
    window.setTimeout(generateItems, 5000); // wait for 5 secs before starting updates
});

/*]]> */
</script>
</html>

In case what I am trying to do is not clear from the code above, this is what I’m trying to do:

  1. Generate a random number of items
  2. Update the ‘list’ of items (held in element with id ‘container’). The update should be done so that the latest generated data are placed BEFORE the older items that were in the list (so visually, they appear ‘on top’)
  3. Ensure that element with id ‘container’ has no more than 10 items in it at any time – with only the LATEST (i.e. TOPMOST) 10 items being left in the ‘list’
  • 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-26T09:24:57+00:00Added an answer on May 26, 2026 at 9:24 am

    Not really sure what your looking for, but try this : http://jsfiddle.net/x36Bm/1/

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

Sidebar

Related Questions

I'm trying to write a small wsgi application which will put some objects to
I've been trying to write a small application which will work with mysql in
Trying to write this small program to help me in my Stats class, everything
I'm trying to write a small Python module which contain some mathematical functions. For
I'm trying to write a small Powershell function that will return some summary data
I am trying to write a small program which has to store and retrieve
I am trying to write a small ruby script that will have two methods
I'm trying to write a small batch script that will delete files old files
I'm trying to write a small utility service that will detect when a item(s)
I'm trying to write a small script which parses and executes Brainfuck code, to

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.