百度空间 | 百度首页 
 
查看文章
 
js thread操作
2008年08月08日 星期五 下午 02:09
/**
* js线程
* _task 页面元数id
* _delay 每次间隔的毫秒数
* _times重复次数-1为无限循环
*/
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);       
    }
}

类别:jscript‖xml‖ajax | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu