How to use JavaScript to obtain the current cpu usage _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to use JavaScript to obtain the current cpu usage. it involves some techniques related to JavaScript for system hardware operations and has some reference value, for more information about how to use JavaScript to obtain the current cpu usage, see the example in this article. We will share this with you for your reference. The details are as follows:

To get the cpu usage of the current system, if hta is allowed, 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 method in a normal webpage for rough estimation?

The idea is to use a timer. assume that the execution interval is 500 ms. when you call it, you can determine the time from the start time to the execution time, if the difference between the two is = 500, it can be considered as normal. if the difference is greater than 500, the cpu is busy for different values (because different machines, the CPU processing capability is not the same, so this is only a rough estimation that is not completely accurate and highly dependent on some system parameters)

function cpuSimulator() { var J = 100,  getNow = function() {   return new Date().getTime();  }; !(function() {  var I = document.createElement("p"),   s = 50,   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 * 100 + "%";   },   t = setInterval(fn, 500),   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 more accurately, you can only adjust the timer execution time and the comparison of the difference.

Some people have already written such an example on ajax.com a few years ago. It is relatively simple to use. enter the following in the address bar of the web page to be monitored:

The code is 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 '));

A small icon appears in the upper-right corner of the current page, which is divided into five levels, representing the status of the current cpu height task (if it is too high, it indicates that the CPU usage is relatively high, and only one rough estimate can be made, not very accurate)

Here is an online example:

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.