javascript線程類比【原創】

來源:互聯網
上載者:User
  在javascript中,是沒有線程的,只能類比一個了,前些日子寫了個,現在把它貼出來。
  thread.js:

/**
 * 線程管理類
 * @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)//無限迴圈
        {
            _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);        
    }
}

  例子如下:

<html>
<head>
<title>測試</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>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.