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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:55:52+00:00 2026-05-30T05:55:52+00:00

I have a Google Chrome extension that upon pressing a button executes an xmlhttp

  • 0

I have a Google Chrome extension that upon pressing a button executes an xmlhttp request to a server. I’m currently handling the error if the server is down. What I want to achieve is to retry the request after an increasing number of seconds. That’s how I do it:

var execute = function(method, url, i){
            //if timeout doesn't exist, create one
            if(!i){
                i = 1000;
            }

            var xmlhttp = new XMLHttpRequest(); 
            xmlhttp.open(method, url, true);

            xmlhttp.onreadystatechange = function(){
                if (xmlhttp.status != 200)  {
                    // Handle error, retry request
                    console.log("xmlhttp.status = " + xmlhttp.status + " and xmlhttp.readyState = " + xmlhttp.readyState);
                    setTimeout("execute('"+method+"', '"+url+"', "+i*2+")", i);
                    return;
                }
            };

            xmlhttp.send(null);
        }

Basically if there is some kind of problem I retry again the request. If the server is up, there is no problem at all, but if the server is down JavaScript throws me an error saying:

PUT http://localhost:3000/buy/2/id=1329664820124 
executepopup.html:127
(anonymous function)

Which is not really meaningful, but however. The status in the log says “0”, which is pretty lame too. If I turn on the server again while this is going on, it should stop doing this, but instead also if it succeeds (I see a log in the server that tells me that it received the request), it keeps calling the execute method. I don’t know how to stop this recursion if the server turns on. Am I doing something wrong? Is this state equal zero the problem?

Thanks a lot

  • 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-30T05:55:53+00:00Added an answer on May 30, 2026 at 5:55 am

    Solving the problem

    onreadystatechange is triggered for every state change, from state 0 to 4, and many times between. You should only be interested in readyState 4, because the request has fully finished at that point.

    To get your method to work, check whether xmlhttp.readyState == 4:

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status != 200)  {
    

    Fixing your horrible implementation of setTimeout

    setTimeout("execute('"+method+"', '"+url+"', "+i*2+")", i); is not the right way to call a function again. Since you’re developing in a Chrome extension, you can use the following format for setTimeout:

    setTimeout(execute, i, method, url, i*2);
    // Calls execute(method, url, i*2) after i milliseconds.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While creating a Google Chrome extension, I have code that must remain in a
I currently looking to build a Google Chrome Extension that fetches bookmarks from the
I work on Google Chrome extension for facebook. I have a button and if
I have created a simple Google chrome extension that overrides CSS classes using the
I am making a Google Chrome extension and I would like to have my
I am working on creating a Google Chrome extension. We have it included in
Note: I'm using Google Chrome Currently I have this test page http://www.evecakes.com/doodles/canvas_size_issue.htm It should
I am experiencing some problems with a google chrome extension I have made. I've
I'm working on a Google Chrome extension. In the popup I have the following
I'm building this Google Chrome extension and I find it a nuisance to have

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.