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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:06:48+00:00 2026-05-19T17:06:48+00:00

I have a problem with the following javascript code. Basically I want to run

  • 0

I have a problem with the following javascript code.
Basically I want to run a background check (check.php) for a process on an Unix server. The execution time of the php and response takes about 1.2-1.5 seconds (did a test in Mozilla) so I’ve come up with the script below to refresh and check what’s going on with that process and change the background of a DIV accordingly.
Right now, after a few refreshes it just hangs, because I think it goes into a loop and it doesn’t wait for the first function to finish and it just piles up.
Any ideas what am I doing wrong? A more simpler way to do it?

<script language="javascript" type="text/javascript">

    function getVal(param)
    {
        var strURL = param;
        var req = new XMLHttpRequest();
        req.open("GET", strURL, false); //third parameter is set to false here
        req.send(null);
        return req.responseText;
    }

    function changeDivImage()
    {   
        var pid = getVal("check.php")
        var imgPath = new String();
        imgPath = document.getElementById("status_body").style.backgroundImage;

            if(pid == 2)
            {
                document.getElementById("status_body").style.backgroundImage = "url(active.png)";
            }
            else
            {
                document.getElementById("status_body").style.backgroundImage = "url(down.png)";
            }
        changetimer();
    }

    function changetimer()
    {
        setInterval( "changeDivImage()", 5000 );
    }
    </script>
  • 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-19T17:06:49+00:00Added an answer on May 19, 2026 at 5:06 pm

    setInterval will keep calling the funciton over and over, so calling the changetimer funciton again after the changeDivImage returns will stack up more and more unwanted functions. try this:

    <script language="javascript" type="text/javascript">
    
        function getVal(param)
        {
            var strURL = param;
            var req = new XMLHttpRequest();
            req.open("GET", strURL, false); //third parameter is set to false here
            req.send(null);
            return req.responseText;
        }
        //this function calls it self automatically and again everytime 
        //but only when it finishes its operations.
    
    (function changeDivImage()
    {   
        var pid = getVal("check.php")
        var imgPath = new String();
        imgPath = document.getElementById("status_body").style.backgroundImage;
    
            if(pid == 2)
            {
                document.getElementById("status_body").style.backgroundImage = "url(active.png)";
            }
            else
            {
                document.getElementById("status_body").style.backgroundImage = "url(down.png)";
            }
          setTimeout(changeDivImage, 5000);
    })();
    

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

Sidebar

Related Questions

I have translated the following Javascript code to Java. The problem occurs at sib;
I have a problem with the following code: for(i = 0;(i - 1)< n;i++)
I have the following javascript. Problem is if I enter one row in the
I have the following problem: I have an HTML textbox ( <input type=text> )
I have the following problem using subversion: I'm currently working on the trunk of
I have the following problem using template instantiation [*]. file foo.h class Foo {
I have the following problem in my Data Structures and Problem Solving using Java
I have the following problem: I open the dialog, open the SIP keyboard to
I have the following problem. If I query values with a keyfigure which is
I have a problem with the following Linq query using Entity Framework: from o

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.