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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:59:10+00:00 2026-06-09T13:59:10+00:00

I’m having an issue with some asynchronous JavaScript code that fetches values from a

  • 0

I’m having an issue with some asynchronous JavaScript code that fetches values from a database using ajax.

Essentially, what I’d like to do is refresh a page once a list has been populated. For that purpose, I tried inserting the following code into the function that populates the list:

var timer1;
timer1 = setTimeout([refresh function], 1000);

As you might imagine, this works fine when the list population takes less than 1 second, but causes issues when it takes longer. So I had the idea of inserting this code into the function called on the success of each ajax call:

clearTimeout(timer1);
timer1 = setTimeout([refresh function], 1000);

So in theory, every time a list element is fetched the timer should reset, meaning that the refresh function should only ever be called 1 second after the final list element is successfully retrieved. However, in execution all that happens is that timer1 is reset once, the first time the second block of code is reached.

Can anybody see what the problem might be? Or if there’s a better way of doing this? Thanks.

==========

EDIT: To clear up how my ajax calls work: one of the issues with the code’s structure is that the ajax calls are actually nested; the callback method of the original ajax call is itself another ajax call, whose callback method contains a database transaction (incorrect – see below). In addition, I have two such methods running simultaneously. What I need is a way to ensure that ALL calls at all levels have completed before refreshing the page. This is why I thought that giving both methods one timer, and resetting it every time one of the callback methods was called, would keep pushing its execution back until all threads were complete.

Quite honestly, the code is very involved– around 140 lines including auxiliary methods– and I don’t think that posting it here is feasible. Sorry– if no one can help without code, then perhaps I’ll bite the bullet and try copying it here in a format that makes some kind of sense.

==========

EDIT2: Here’s a general workflow of what the methods are trying to do. The function is a ‘synchronisation’ function, one that both sends data to and retrieves data from the server.

  • I. Function is called which retrieves items from the local database
  • i. Every time an item is fetched, it is sent to the server (ajax)
  • a. When the ajax calls back, the item is updated locally to reflect
    its success/failure

  • II. A (separate) list of items is retrieved from the local database

  • i. Every time an item is fetched, an item matching that item’s ID is fetched from the server (ajax)
  • a. On successful fetch from server, the items are compared
  • b. If the server-side item is more recent, the local item is updated

So the places I inserted the second code block above are in the ‘i.’ sections of each method, in other words, where the ajax should be calling back (repeatedly). I realize that I was in error in my comments above; there is actually never a nested ajax call, but rather a database transaction inside an ajax call inside a database transaction.

  • 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-09T13:59:12+00:00Added an answer on June 9, 2026 at 1:59 pm

    You’re doing pretty well so far. The trick you want to use is to chain your events together, something like this:

    function refresh()
    {
        invokeMyAjaxCall(param1, param2, param3, onSuccessCallback, onFailureCallback);
    }
    
    function onSuccessCallback()
    {
        // Update my objects here
    
        // Once all the objects have been updated, trigger another ajax call
        setTimeout(refresh, 1000);
    }
    
    function onFailureCallback()
    {
        // Notify the user that something failed
    
        // Once you've dealt with the failures, trigger another call in 1 sec
        setTimeout(refresh, 1000);
    }
    

    Now, the difficulty with this is: what happens if a call fails? Ideally, it sounds like you want to ensure that you are continually updating information from the server, and even if a temporary failure occurs you want to keep going.

    I’ve assumed here that your AJAX library permits you to do a failure callback. However, I’ve seen some cases when libraries hang without either failing or succeeding. If necessary, you may need to use a separate set of logic to determine if the connection with the server has been interrupted and restart your callback sequence.

    EDIT: I suspect that the problem you’ve got is a result of queueing the next call before the first call is done. Basically, you’re setting up a race condition: can the first call finish before the next call is triggered? It may work most times, or it may work once, or it may work nearly all the time; but unless the setTimeout() is the very last statement in your “response-processing” code, this kind of race condition will always be a potential problem.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a view passing on information from a database: def serve_article(request, id): served_article
I have just tried to save a simple *.rtf file with some websites and
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.