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

  • Home
  • SEARCH
  • 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 8477069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:21:07+00:00 2026-06-10T18:21:07+00:00

I have a problem with a for loop in jQuery. I’m trying to build

  • 0

I have a problem with a for loop in jQuery. I’m trying to build an ads section which will rotate dynamically to the last ad.

Unfortunately, jQuery gives me a static value for my incrementor.

Here is my code

function ads(){

   var i = 0;

   for(i=0; i<=3; i++){    
      $("#adBox").delay(5000).hide("slide", { direction: "up" }, 1000);    
      $("#adBox").delay(1000).show("slide", { direction: "right" }, 1000);    
      $("#adContent").load("adPage" +i+".php");
   }
}    
ads();
  • 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-10T18:21:09+00:00Added an answer on June 10, 2026 at 6:21 pm

    There are a couple of things going on here that might be relevant:

    • There’s nothing obviously wrong with the for loop syntax. If you add console.log("i: " + i); inside the loop, you should see four lines written to the browser’s JavaScript console when this code executes, e.g.:
    i: 0
    i: 1
    i: 2
    i: 3
    
    • Each time the loop executes, the following things will happen, in this order:

      1. A delay of 5 seconds is added to the animation queue for element #adBox, then a slide up animation is added to the queue.
      2. Immediately after those items are added to the queue, without waiting for the delay or animation to begin, another delay and a slide right animation are added to the same element’s queue.
      3. Without waiting for either of the delays or animations to execute, a call is made to get "adPage" + i + ".php". Once the page is returned, it will be loaded into element #adContent. However, the .load() method returns immediately, without waiting for the page to be retrieved or loaded.
    • The for loop probably completes all four iterations before the first call to .load() has even completed.

    • Finally, just a note about terminology: The for loop is built-in to JavaScript. It has nothing to do with jQuery, which is a JavaScript library.

    I suspect that what you really want to happen is something like this:

    function ads(i) {
        if (i <= 3) {
            $("#adBox").delay(5000).hide("slide", { direction: "up" }, 1000, function() {
                $("#adBox").delay(1000).show("slide", { direction: "right" }, 1000, function() {
                    $("#adContent").load("adPage" + i +".php", function() {
                        ads(i + 1);
                    });
                });
            });
        }
    }
    

    Those anonymous function arguments I’m passing to hide, show, and load are callback functions. They will be executed only after each function completes.

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

Sidebar

Related Questions

I'm using the jQuery audio player 'jPlayer', I have a loop which creates a
I have a problem with jQuery, for some reason, that code makes infinite loop:
I have a method, which will accept a parameter of a JQuery Object and
I have one problem with my function in jQuery. First I will explain what
I have a simple problem regarding a loop in a Rails controller. Here's the
I have a problem with a continue statement in my C# Foreach loop. I
I have an interesting problem. I wrote the following perl script to recursively loop
I have the following loop which is giving me problems $(#divResults).append('<table>'); $.each( results.d, function(
I have problem in adding autocomplete plugin to multiple text inputs in loop (no
I have a jquery loop where it displays buttons A - Z but what

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.