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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:16:32+00:00 2026-06-14T06:16:32+00:00

I’m trying to write a script to sequentially pull data from printers that have

  • 0

I’m trying to write a script to sequentially pull data from printers that have a web interface. There are over 500 on a large local network, hence the reason I’d like to call out to the next one once the previous returns (successful or not). I’m having difficulties wrapping my head around callbacks …if that’s the best solution.

Here’s where I currently stand (be kind, this is my intro to jQuery/Javascript and this is a proof test). Also, is there a way to build an array from the resulting calls? The reason I’ve turned to Javascript is that I could not get PHP to behave with the execution times.

Oh, you’ll see ASYNC: true below only because I tried w/ false and it did not work (return anything).

function pingDevice(prnList) {
    prnList = ["www.google.com", "amazon.com", "facebook.com", "as923f.com"];
    document.getElementById("myDiv").innerHTML="please wait...";

    $(prnList).each( function(index,printer) {
        $.ajax({
            type: "GET",
            dataType: "html",
            url: "pingTest.php?ping="+printer,
            async: true,
            success: function (data) {
                $('#myDiv').append( $('<div class="prn"></div>').html(data) );
                $prnResults[printer] = data;
            },
            error: function () {
                $prnResults[printer] = data;
            }
        });
    });
    $('#myDiv').append( $('<div class="prn"></div>').$prnResults );
}
  • 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-14T06:16:34+00:00Added an answer on June 14, 2026 at 6:16 am

    It looks like there are two things you want your code to do.

    1. Only request data for the next printer when the request for the previous printer has returned
    2. Only update the DOM once all the requests have returned.

    Unfortunately, the code you have does neither of these things. 🙁

    $.each is a way to iterate over an array-like thing, but it won’t wait until the request returns before moving on the next item in the list. The ajax requests happen asynchronously, which means you call them and move on to the next operation in your code. When they are done, they will call back either the success or error functions, but this code will never block while requests are happening.

    If you want to fire off sequential requests, you can create some kind of helper function that lets you loop through an array, and only calls the function on the next item in the array when you know you are done with the first one. If you want to do some action when they are all finished, then you need one more function that calls back when all the items in the array are done being processed.

    Here is a small example of how you can do asynchronous things in sequence.

    var data = ['1', '2', '3', '4'];
    
    function doAsnycStuffSequentially(data, callback, final) {
      var i = 0,
          length = data.length;
    
      function getNextItem() {
        callback(data[i++], done);
      }
    
      function done(err) {
        if (err) {
          return final(err);
        }
    
        // Only call the final function if there is an error or if we are done looping.
        if (i >= data.length) {
          return final();
        }
    
        getNextItem();
      }
    
      getNextItem();
    }
    
    doAsyncStuffSequentially(data, function(item, done) {
      console.log("item is ", item);
      // simulate some asynchronous operation
      setTimeout(done, 1000);
    }, function(err) {
      console.log("done with all and error is", err);
    });
    

    To apply this pattern to your own code, you just define your own callback function that gets called on each item in the array, like this:

    var printerData = {},
        printerNames = ['hurp', 'durp'];
    
    function getPrinterInfo(printerName, done) {
      $.ajax({
          type: "GET",
          dataType: "html",
          url: "pingTest.php?ping="+printer,
          success: function (data) {
            printerData[printerName] = data;
            done();
          },
          error: function (jqXHR, textStatus, err) {
            console.error("error getting printer status", err);
            done(err);
          }
      });
    }
    
    doAsyncStuffSequentially(printerName, getPrinterInfo, function(err) {
      console.log("done getting printer names and error was", err);
      console.log("printerData is", printerData);
    });
    

    The key is that you are calling the done function in the success and error callbacks. This signals to the doAsyncStuffSequentially function that an async operation has finished, and it should either start the next one or call the final function if we are done or an error occurred.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have a text area in my form which accepts all possible characters from
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.