Let the CPU in the Task Manager dance the JS code _javascript tips

Source: Internet
Author: User
Tags cpu usage
Previously in < programming beauty > read the article about controlling CPU usage in Task Manager. I feel very mean. Always wanted to do it myself once. But I have a limited level of C language. Therefore, it is not possible to practice this process directly by finding code.
But I've been using JavaScript a lot lately, so I thought about using JavaScript. The results are not bad. The code is below, just build an HTML file on the desktop, put the code in, and open it with a browser to see. Because I use the machine is dual-core, so the effect is not very good. Hope to have a master to help me improve this program.
Copy Code code as follows:

<script type= "Text/javascript" >
var SPLIT = 0.01;
var COUNT = 200;
var PI = 3.14159265;
var INTERVAL = 150;
var busyspan=new Array (); Array of busy times
var idlespan=new Array (); Array of idle times
function ShowMsg ()
{

var half=interval/2;
var radian = 0.0;
var i=0;
for (i = 0;i<count; i++)
{
Busyspan[i] = (half + (Math.sin (PI * radian) * half));
Idlespan[i] = Interval-busyspan[i];
Radian+=split;
}
Cystime ();
return 0;
}
var j=0;var flag=0;
function Cystime ()
{
J=j%count;
Busy
flag=0;
var nowobj=new Date ();
var nowms=nowobj.gettime ();
var newms=nowms;
while ((NEWMS-NOWMS) <busyspan[j])
{nowobj=new Date (); Newms=nowobj.gettime ();}

Idle
var idlt=settimeout (' Cystime () ', idlespan[j]);
j + +;
}
</script>
<body >
<input onclick= "showmsg ()" type= "button" value= "Start"/>
</body>

If you want to get the CPU approximately flat-curve, you can
Task Manager-> Performance,-> Resource Monitor view

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.