JavaScript how to get current CPU usage _javascript tips

Source: Internet
Author: User
Tags setinterval cpu usage

The examples in this article describe how JavaScript obtains current CPU usage. Share to everyone for your reference, specific as follows:

To get the current system CPU usage, if you allow an HTA, you can use the following code to get the current CPU usage (more accurate)

SetInterval (function () {
 var locator=new activexobject ("WbemScripting.SWbemLocator");
 var service=locator. ConnectServer (".");
 var cpu=new Enumerator (service. ExecQuery ("SELECT * from Win32_Processor")). Item ();
 Document.title = CPU. Loadpercentage;
}, 1000);

If the above method is not allowed, is there a more appropriate way to make a rough estimate on the normal Web page?

The idea is to use a timer, assuming interval execution time is 500ms, when it is invoked, it is judged by the time from the start timer to this execution, if the difference between the two = = 500 is generally considered to be relatively normal, if > 500+ for different values to determine the CPU's busy state (because different machines , CPU processing capacity is not the same, so this is only a rough estimate is not completely accurate, some of the parameters of the system have a strong dependence on

function Cpusimulator () {
 var J = m,
  getnow = function () {return
   new Date (). GetTime (
 ); (function () {
  var I = document.createelement ("div"),
   s =,
   fn = function (l) {
    L = 1;
    var now = Getnow ();
    var c = 1;
    while (c < J) {
     if (now > D + c*s) {
      l++
     }
     C + +;
    }
    D = Getnow ();
    i.innerhtml = "CPU:" + l/j * + "%";
   },
   t = setinterval (FN, +),
   D = Getnow ();
   I.style.csstext = "width:80px; height:20px; Position:fixed!important; _position:absolute; top:10px; right:10px; border:1px solid #406c99; padding:2px; Color: #f00; ";
   Document.body.appendChild (I);
   fn ();
 }) ();
}

If you want to determine the more accurate, it can only be the timing of the timer and the difference between the comparison of the adjustment.

A few years ago, someone had already written such an example on ajax.com. It's easy to use, just type in the address bar of the Web page you need to monitor:

Copy Code code as follows:
JavaScript: (function (s) {s.type= ' text/javascript '; s.src= ' http://www.3site.eu/JPU/IJPU.js '; document.getElementsByTagName (' head ') [0].appendchild (s)}) (Document.createelement (' script '));

In the top right corner of the current page, there will be a small icon, which is divided into 5 levels, representing the current CPU height task status (too high to indicate CPU usage is relatively high, only a ballpark estimate, not very accurate)

Here's an online example:

 
 

Its entire IJPU code is relatively short, you can refer to:

/* (c) Andrea Giammarchi * * (
function (J, P, U) {
 var a = ' appendchild ',
  c = ' createelement ',
  e = ' Addeven Tlistener ',
  d = document,
  L = "Load",
  w = window;
 (function () {
  var i = setinterval (function (l) {
   L = 1;
   D = new date-d;
   if (D > m) l++;
   if (D > 650) l++;
   if (D >) l++;
   if (D >) l++;
   S (j = L < J--j:l);
   D = new Date
  },
   B = d.body,
   s = function () {
    i.classname = U + J
   },
   I = d[c] (' P '), 
   c = d[c] (' link '),
   d = new Date;
  C.rel = ' stylesheet ';
  C.type = ' text/css ';
  C.href = P + U + '. css ';
  S (B[a] (C), B[a] (I)); /* @cc_on setinterval (function () {i.style.top=document.body.scrolltop+ "px"},50) @*/}) (
 ) (
5, ' http:// www.3site.eu/JPU/', ' jpu ');

I hope this article will help you with JavaScript programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.