Home > Article > Web Front-end > JavaScript method to get current cpu usage_javascript tips
The example in this article describes how to obtain the current cpu usage using JavaScript. Share it with everyone for your reference, the details are as follows:
If you want to get the current system CPU usage, if hta is allowed to be used, 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 to make a rough estimate on a normal web page?
The idea is to use a timer. Assume that the interval execution time is 500ms. When calling it, judge the time from the start of timing to this execution. If the difference between the two == 500, it can basically be considered normal. If > 500+, use different values to determine the busy status of the CPU (because different machines have different CPU processing capabilities, so this is only a rough estimate at most and is not completely accurate, and has a strong dependence on some parameters of the system. Sex)
function cpuSimulator() { var J = 100, getNow = function() { return new Date().getTime(); }; !(function() { var I = document.createElement("div"), 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 make a more accurate judgment, you can only adjust the execution time of the timer and compare the difference.
Someone had written such an example on ajax.com a few years ago. It is relatively simple to use, just enter directly in the address bar of the webpage you want to monitor:
A small icon will appear in the upper right corner of the current page, which is divided into 5 levels, respectively representing the status of the current CPU high task (too high means that the CPU usage is relatively high, and only a rough estimate can be made. Not very precise)
Here is an online example:
<html> <head> <script type="text/javascript" src="http://www.3site.eu/JPU/JPU.js"> <!--// JPU - by WebReflection //--> </script> <script type="text/javascript"> <!--// function randomString(){ for(var i = 0, a = []; i < Math.ceil(Math.random() * 123456789); i++) a[i] = String.fromCharCode(Math.round(Math.random() * 50) + 30); return a.join(""); }; onload = function(){ setInterval(function(){ var div = document.getElementById("demo"); div.appendChild(document.createTextNode(randomString())); }, 300); }; //--> </script> <style type="text/css"> div{overflow:hidden;width:80%;height:80%;color:#FFF;} </style> </head> <body> <div id="demo"></div> </body> </html>
The entire IJPU code is relatively short, you can refer to it:
/* (C) Andrea Giammarchi */ (function (J, P, U) { var a = 'appendChild', c = 'createElement', e = 'addEventListener', d = document, l = "load", w = window; (function () { var i = setInterval(function (l) { l = 1; D = new Date - D; if (D > 700) l++; if (D > 650) l++; if (D > 600) l++; if (D > 550) l++; s(J = l < J ? --J : l); D = new Date }, 500), 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 be helpful to everyone in JavaScript programming.