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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:39:08+00:00 2026-06-16T00:39:08+00:00

I am working on this CPU monitor using PHP and JQuery – http://nereus.rikkuness.net/admin/cpu2.php It’s

  • 0

I am working on this CPU monitor using PHP and JQuery – http://nereus.rikkuness.net/admin/cpu2.php

It’s working exactly as I intend it to work with one slight problem. Due to the command used which polls the current CPU usage there is a delay from the JQuery calling for a value update and the update actually arriving of 1 second. The knock-on effect of this is that when the bar animates it’s always a second behind because the first time it tries to resize it still hasn’t received the new value and so resizes itself based on the last value it received.

Can anyone think of any way in which I can get it to animate as soon as the value updates, regardless of when the value is actually received?

Thanks guys, you’re the best! 🙂

Code is as follows if you don’t want to view source on the live page:

var auto_refresh = setInterval(
    function(){
        height = 100;
        $("#val1").load("cpu.php");
        cpuUsage = $("#val1").html();
        height = cpuUsage * 10;
        barColor = "";

        if(parseInt(height) < 500){
            barColor = "green";
        }else if(parseInt(height) > 800){
            barColor = "red";
        }else{
            barColor = "#febf01";
        }

        $("#val2").animate({
            width: parseInt(height),
            backgroundColor: barColor
        })
}, 1000);
  • 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-16T00:39:09+00:00Added an answer on June 16, 2026 at 12:39 am

    Use the completion function in .load() which will tell you exactly when $("#val1").load("cpu.php"); has completed like this:

    var auto_refresh = setInterval(
        function(){
            $("#val1").load("cpu.php", function() {
                var cpuUsage = $("#val1").html();
                var height = parseInt(cpuUsage * 10, 10);
                var barColor = "";
    
                if(height < 500){
                    barColor = "green";
                }else if(height > 800){
                    barColor = "red";
                }else{
                    barColor = "#febf01";
                }
    
                $("#val2").animate({
                    width: height,
                    backgroundColor: barColor
                })
            });
    }, 1000);
    

    FYI, I also made these additional changes:

    1. Added var to make your variables be local variables, not implicit global variables.
    2. I factored out the parseInt() on the height so it is only called in one place instead of 4 places.
    3. I added the radix parameter on the parseInt() which should always be used.
    4. Remove the extra initialization of the height variable which is not needed.

    I would actually suggest a different implementation that doesn’t start a timer for the next iteration until the last iteration is done. As you have it now, if your cpu.php call ever takes more than 1 second to respond, you will pile up multiple calls all in flight at once. Instead, you can start the next iteration’s timer when the previous one finished.

    var usageTimer;
    var usageContinue = false;
    
    function stopUsage() {
        clearTimeout(usageTimer);
        usageContinue = false;
    }
    
    // getUsage runs continuously until stopUsage is called
    function getUsage() {
        var start = new Date().getTime();
        $("#val1").load("cpu.php", function() {
            if (usageContinue) {
                var cpuUsage = $("#val1").html();
                var height = parseInt(cpuUsage * 10, 10);
                var barColor = "";
    
                if(height < 500){
                    barColor = "green";
                }else if(height > 800){
                    barColor = "red";
                }else{
                    barColor = "#febf01";
                }
    
                $("#val2").animate({
                    width: height,
                    backgroundColor: barColor
                })
                // start the next no sooner than 1 second from when the last one was started
                var end = new Date().getTime();
                // if the .load() call already took more than a second, then just start the next one now
                if (end - start > 1000) {
                    getUsage();
                } else {
                    // otherwise, start the next one 1 second after the previous one was started (to try to get one every second)
                    usageTimer = setTimeout(getUsage, end - start);
                }
            }
        });
    }
    getUsage();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this (working) CPU code: #define NF 3 int ND; typedef double (*POT)(double
I do not get the last version of node-http-proxy working (this used to work
So this was working on this project a few months ago. I'm using Google
I'm working on a rather CPU-intensive web application. I've started using -webkit-transform: translate3d(0, 0,
I am working with PHP classes and objects now. In this question the names
I'm using this piece of code that is working correctly to detect iOS devices
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
I am tired of working this out myself , i cannot understand the way
i am new in programming under linux and trying to get working this code:
I'm a LAMP guy and ended up working this small news module for an

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.