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>