zoukankan      html  css  js  c++  java
  • javascript线程模拟【原创】

      在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>
  • 相关阅读:
    strpos 判断字符串是否存在
    TP 自动验证
    label 标签的用法,点label选中单选、复选框或文本框
    str_replace 替换 小技巧
    数据库文件MDF的空间占满了,没有自动增长是怎么回事?
    (4.7)mysql备份还原——深入解析二进制日志(3)binlog的三种日志记录模式详解
    (4.6)mysql备份还原——深入解析二进制日志(2)binlog参数配置解析
    (1.16)mysql server优化之buffer pool
    COALESCE函数
    linux网络设置和虚拟机克隆转移之后网卡找不到
  • 原文地址:https://www.cnblogs.com/zxub/p/438261.html
Copyright © 2011-2022 走看看