最近再做一个程控程序,其中一个功能就是用户接通后开始计时,断开后计时停止
效果如下:
var CallTime =function(){
this.time = 0;
this.Timer = null;
var that = this;
this.getTime=function(){
this.Timer = setTimeout(function(){
that.time+=1;
callTime.getTime(that.time);
console.log(that.checkTime(that.time));
},1000)
};
// 关闭定时函数
this.closeTime=function(){
clearTimeout(this.Timer);
this.time = 0;
this.Timer = null;
};
// 检测时间 输入秒钟格式化成 时:分:秒
this.checkTime = function(t){
var h,m,s;
h=parseInt(t/3600);
m = parseInt(t%3600/60);
s=parseInt(t%60);
return that.two(h)+":"+that.two(m)+":"+that.two(s);
}
// 补位 格式化时间 00:00:00
this.two = function(i) {
if (i < 10) {
i = "0" + i;
}
return i;
};
// 预留set函数,设置时间
this.setTime = function(time){
this.time = time;
}
}
var callTime = new CallTime();
// 建立通话
callTime.getTime();
// 结束通话
// callTime.closeTime();
// 设置秒钟时间
// callTime.setTime(0);
将代码拷贝到控制台,即可看到运行结果。