您的位置:首页 > Web前端 > JavaScript

模拟线程Timer(JS)

2015-05-15 19:30 274 查看
<script language="JavaScript" type="text/javascript">

var Timer = function(runnable){
   this._runnable=(runnable=(runnable||this));
this._runFunc0 = function(){
    runnable['run'].apply(runnable);
}
this._interval = 1000;
};
Timer.prototype.run = function(){
if(this._time_count==null){
    this._time_count=0;
};
document.body.innerHTML=document.body.innerHTML+'Timer: '+(this._time_count++)+'<BR>'
};
Timer.prototype.start = function()
{
   this._timeId = window.setInterval(this._runFunc0, this._interval);
};
Timer.prototype.sheep = function(st)
{
var sheepTime = st || 500;
   window.clearTimeout(this._timeId);
   var _this = this;
   window.setTimeout((function(){_this['start'].apply(_this);}), sheepTime );
};

var t = new Timer();
t.start();

</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: