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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:23:00+00:00 2026-05-25T17:23:00+00:00

I have written a bit of jQuery to loop through items in a array

  • 0

I have written a bit of jQuery to loop through items in a array an display a random number and then move on to display another number. The problem is I want to put a delay inside the loop so that it shows the number for 2 seconds and then moves of to the next. so you see a random number it holds for 2 seconds and then moves on to the next. I have tried putting a timeout inside the loop but that didn’t work. Any help would be amazing thanks.

function RandomNumberGenerator(){

    var myArray = new Array(99);

        for (var i=0; i< 99; i++) {
            myArray[i] = Math.floor(Math.random()*100)
            myArrayTwo[i] = Math.floor(Math.random()*100)
        }

        for (var i=0; i< 9; i++) {
            $('li.num').text(function(index) {

            // timeout needs to be here somewhere 

            return (myArray[i]);

            })
        }
});
}
  • 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-25T17:23:00+00:00Added an answer on May 25, 2026 at 5:23 pm

    First of all you need to put the code that does the work (displays the random number and decides if it should continue) inside its own function. This function can also be a local variable inside RandomNumberGenerator:

        var displayNumber = function() {
            $('li.num').text(myArray[i++]);
            if (i < 100) {
                setTimeout(displayNumber, 2000);
            }
        };
    

    The function above puts a number in li.num, increments i, and tells the browser to call the function again (using setTimeout) in two seconds — but only if we have shown less than 100 numbers.

    So when this function (displayNumber) is called for the first time, it displays one number and sets things up to call itself again after 2 seconds (to display another number, and so on). If it has displayed 100 numbers already it does not set the call up again, so at that point the repetition stops.

    So you can now do:

        var myArray = new Array(99);
        for (var i=0; i< 99; i++) {
            myArray[i] = Math.floor(Math.random()*100);
        }
    
        var i = 0;
        var displayNumber = function() {
            $('#foo').text(myArray[i++]);
            if (i < 10) {
                setTimeout(displayNumber, 2000);
            }
        };
    
        displayNumber(); // to get the ball rolling
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have written this simple bit of jQuery that swaps out the containing ul's
I have written a bit of jQuery that hides a certain element on page
I have written a simple jquery script for a content slider. My problem is
Hi I have a bit of a problem. I have written a custom component
I have some old code written in C for 16-bit using Borland C++ that
I have just been re-working an old bit of compiler-like code written using bison.
I'm a new bit in Qt... I have a Qt GUI application (written by
I have written a site in Prototype but want to switch to jQuery. Any
I have written quite a bit of code which uses the Linq2Sql table relationships

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.