Javascript thread Simulation

Source: Internet
Author: User
In JavaScript, there is no thread, and only one can be simulated. I wrote it a few days ago and paste it out now.
Thread. JS :/**
* Thread management class
* @ Author zxub 2006-06-12
*/
Function thread (_ task, _ delay, _ times)
{
This. runflag = false;
This. busyflag = false;
This. taskargs = array. Prototype. Slice. Call (arguments, 3 );

If (_ times! = Undefined)
{
This. Times = _ times;
}
Else
{
This. Times = 1;
}

VaR _ point = this;

This. timerid =-1;

This. Start = function ()
{
If (this. runflag = false)
{
This. timerid = Window. setinterval (_ point. Run, _ delay );
This. runflag = true;
}
}

This. Run = function ()
{
If (_ point. busyflag) return;
If (_ point. Times =-1) // Infinite Loop
{
_ Task (_ point. taskargs );
}
Else if (_ point. Times> 0)
{
_ Task (_ point. taskargs );
_ Point. Times-= 1;
If (_ point. Times = 0)
{
Window. clearinterval (this. timerid );
}
}
}

This. Sleep = function ()
{
This. busyflag = true;
}

This. Resume = function ()
{
This. busyflag = false;
}

This. Abort = function ()
{
Window. clearinterval (this. timerid );
}
}

Example: <HTML>
<Head>
<Title> test </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<SCRIPT type = "text/JavaScript" src = "thread. js"> </SCRIPT>
<Style type = "text/CSS">
<! --
Body, TR, TD {font-size: 12px ;}
-->
</Style>
</Head>
<Body>
<SCRIPT>
VaR func = function (_ o)
{
Document. getelementbyid (_ o). innerhtml = parseint (document. getelementbyid (_ o). innerhtml) + 1;
}
VaR T1 = new thread (func, 50,121, "T1 ");
VaR t2 = new thread (func, 200,20, "T2 ");
</SCRIPT>
<Input type = "button" value = "start1" onclick = 'T1. Start (); '> </input>
<Input type = "button" value = "sleep1" onclick = 'T1. Sleep (); '> </input>
<Input type = "button" value = "resume1" onclick = 'T1. Resume (); '> </input>
<Input type = "button" value = "abort1" onclick = 'T1. Abort (); '> </input>
<Input type = "button" value = "start2" onclick = 't2. Start (); '> </input>
<Input type = "button" value = "sleep2" onclick = 't2. Sleep (); '> </input>
<Input type = "button" value = "resume2" onclick = 't2. Resume (); '> </input>
<Input type = "button" value = "abort2" onclick = 't2. Abort (); '> </input>
<Div id = "T1"> 0 </div> | <Div id = "T2"> 0 </div>
<Input type = "button" value = "t1.timerid" onclick = 'alert (t1.timerid); '> </input>
<Input type = "button" value = "t2.timerid" onclick = 'alert (t2.timerid); '> </input>
</Body>
</Html>

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.