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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:56:09+00:00 2026-06-11T10:56:09+00:00

I am trying to loop through a couple of different values. The idea is

  • 0

I am trying to loop through a couple of different values. The idea is that if a user clicks a button the loop should start to run, once it runs it should show a piece of code(in the example some basic text). I am trying to show a sort of dialog with different information, the duration depending on the time values(milliseconds), so the first starts with 3 seconds, right after that the second should be visible for 8 seconds, after this the last one should run for 2 second.

I had to make changes so that every loop the time is different. The example below has a kill switch, so that i can kill the loop. I hope that somebody can figure this out, I have tried a lot of hours and hours.

//plugin options

  step:[
      {
          time: 3000,
          name: 'mike',
          age: 30
      },
      {
          time: 8000,
          name: 'john',
          age: 37
      },
      {
          time: 2000,
          name: 'jessica',
          age: 25
      }
  ]

// the loop

  var timeouts = [];

$('.click').click(function(){
  $.each(options.step, function(i, value){

      var time = value.time;                                                        
      timeouts.push(setTimeout(function(){

          $('#target ul').append('<li>'+value.name+'-'+value.age+'</li>')

      },time));

  });

 });

// reset button

  $('.stop').click(function(){
      $.each(timeouts, function (_, id) {
          clearTimeout(id);
      });
      timeouts = [];
  });

EDIT: the issue is that it needs a extra value(0) in the loop(first value should be a 0), as the the first setTimeout runs after 3 seconds, which means that the rest will use the value of there neighbor.

  • 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-06-11T10:56:10+00:00Added an answer on June 11, 2026 at 10:56 am

    Your script sets 3 timeouts: show Mike in 3s, John in 8s and Jessica in 2s – so Jessica is first. If you want them to happen one at a time, you will need to accumulate these times:

    var timeouts = [],
        accum = 0;
    $.each(options.step, function(i, value){
        accum += value.time;                                                        
        timeouts.push(setTimeout(function(){
            $('#target ul').append('<li>'+value.name+'-'+value.age+'</li>')
        }, accum));
        // move the addition here if you want them to start immediately
    });
    

    (Demo at jsfiddle.net, with immediate start)

    The other way would be to start the next timeout once one just occurs, this would allow easier stop&go:

    var cur = 0,
        $ul = $('#target ul'),
        tid;
    function next() {
        if (cur >= options.step.length) return; // and call a final callback?
        var value = options.step[cur];
        $ul.append('<li>'+value.name+'-'+value.age+'</li>');
        cur++;
        tid = setTimeout(next, value.time);
    }
    function stop() {
        clearTimeout(tid);
    }
    

    (Demo at jsfiddle.net)

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

Sidebar

Related Questions

I'm trying to loop through the results of a function that is returning an
I'm trying to make a loop that will recurse through an array of bytes
Trying to loop through an array of hashes and getting an error message that
I am trying to loop through a table that has a column for customers
I am trying to loop through a query string and pull out certain values
I'm stuck trying to loop through this array of GPS coordinates that puts pins
I'm trying to loop through the arguments that I am passing to a batch
Trying to loop through multiple spatial extents to create 17 unions of 4 different
m trying to loop through 3 resultsets and compare their values. bt its throwing
I'm trying to loop through a list of results from MySQL and display all

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.