[记录] javascript 对象中使用setTimeout
2024-08-25 07:34:17
参考:Javascript对象中关于setTimeout和setInterval的this介绍
使用最后一个方法终于弄好了,简直了,在对象中使用setTimeout原来是这样的
做的是分钟倒计时,倒数3分钟
function clock(){ this.min = 3; //倒数的分钟
this.speed = 1000; //setTimeout时间
var timeId = ""; //保存setTimeout对象
this.time = this.min * 60;//3分钟等于180s
this.move = function(){
var that = this;//保存当前对象this
this.time = parseInt(that.time);
var minute = 0; //分
var seconds = 0; //秒 if(this.time > 60){
minute = parseInt(that.time / 60);
seconds = parseInt(that.time % 60);
}else{
minute = 0;
seconds = parseInt(that.time % 60)
} var txt = ((minute < 10) ? "0" + minute : minute) + " : " + ((seconds < 10) ? "0" + seconds : seconds); timeId = setTimeout(function(){
that.move();
},that.speed); console.log(that.time);
that.time--;
console.log(txt); if(that.time <= 0){
clearTimeout(timeId);
};
},
this.stop = function(){
clearTimeout(timeId);
}
}
使用:
var eva3_clock = new clock();
eva3_clock.move();
最新文章
- SpringMVC学习记录4
- 人工智能 - AI
- Gson解析复杂的json数据
- x265编译
- PushKit和传统长连接方式的比较
- 最简单理解CGI,FastCGI,WSGI
- BaseActivity的定义——作为所有Activity类的父类
- 工作经常使用的SQL整理
- MySQL导出csv乱码问题的解决
- CF1059C Sequence Transformation
- 说说Python中的闭包
- goroutine和channel
- NuGet 构建服务器与常用命令
- HTML DOM学习
- [UWP 自定义控件]了解模板化控件(5):VisualState
- sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)
- 给大家推荐一个好的android模拟器genymotion,非常流畅,跟真机差不多
- sql语句优化总结
- Reflector反编译.NET文件后修复
- BZOJ4057 [Cerc2012]Kingdoms