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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:22:56+00:00 2026-05-16T20:22:56+00:00

I have made a loop in my JavaScript code and in that loop I

  • 0

I have made a loop in my JavaScript code and in that loop I use an XMLHttpRequest with a callback method but I’m having a problem with it.

When my loop ends the callback executes. This is not good, I need the callback to execute after calling a function, not after looping. How can I do this?

Somebody on a blog wrote that I can use an async method. How can I use an async method in this condition?

for (var i = 0; i < stuff.length; i++) {
                        var exist = IsstuffExist(stuff[i]);
                        alert(exist);
                    }

the called function show alert();

when i call function then i got alert of my code first and callback alert comes after my code.

how i can stop loop for sometime then i can get back response as callback from called function

if i use in looping

 IsstuffExist(tags[i], function (result) {
                            if (result == true) {
                                $("#txtstuff").append(stuff[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-16T20:22:56+00:00Added an answer on May 16, 2026 at 8:22 pm

    You basically have to change your thinking. You need to change your style from writing code like this:

    for (var i = 0; i < stuff.length; i++) {
        var exist = IsstuffExist(stuff[i]);
        alert(exist);
    }
    

    to writing stuff like this:

    for (var i = 0; i < stuff.length; i++) {
        IsstuffExist(stuff[i], function(exist) {
            alert(exist);
        });
    }
    

    the IsstuffExist function can be written like this:

    function IsstuffExist (stuff, callback) {
        // Do things and once you get the `exist` variable you can
        // pass it to the callback function. Any code that needs
        // to continue processing things can then resume from
        // within the callback function:
        callback(exist);
    }
    

    This technique can be nested, passing the callback function to other callback functions. A concrete example is with ajax calls:

    // I'm using my own ajax library in this example but it's the same
    // if you use other libraries:
    
    function IsstuffExist (stuff, mycallback) {
        // Make ajax call to find out if stuff exist:
        ajax('some/url.com', {
            callback : function (r) {
                var status = r.responseText;
                mycallback(status);
            }
        });
    }
    

    note: I renamed the callback for the function to mycallback to avoid confusion. But in real code I would simply name it callback.

    So now instead of writing code like this:

    for (var i = 0; i < stuff.length; i++) {
        if(IsstuffExist(stuff[i])) {
            doSomethingAndUpdateTheHTML();
        }
    }
    

    you write it like this:

    for (var i = 0; i < stuff.length; i++) {
        IsstuffExist(stuff[i],function(exist){
            if (exist) {
                doSomethingAndUpdateTheHTML();
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a code that read data from flash Nand (without filesystem). fd
I have made some code that creates a red border around an image when
I have made a jQuery toggle for a menu that I had in mind.
i have made an application having entity framewrok. It is wpf application, now it
I have made a cart and the cart has remove item button, but the
I have made a quick Jsbin: http://jsbin.com/ujabew/edit#javascript,html,live What i'm trying to achieve is to
Inside of HAML, can we have a loop inside the :javascript region? This will
I have made a custom loop in WordPress and for some reason, the date
I have made some research on Stackoverflow about reverse for loops in C++ that
I have the code as per below. This retrieves some data to use for

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.