参考: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();

最新文章

  1. SpringMVC学习记录4
  2. 人工智能 - AI
  3. Gson解析复杂的json数据
  4. x265编译
  5. PushKit和传统长连接方式的比较
  6. 最简单理解CGI,FastCGI,WSGI
  7. BaseActivity的定义——作为所有Activity类的父类
  8. 工作经常使用的SQL整理
  9. MySQL导出csv乱码问题的解决
  10. CF1059C Sequence Transformation
  11. 说说Python中的闭包
  12. goroutine和channel
  13. NuGet 构建服务器与常用命令
  14. HTML DOM学习
  15. [UWP 自定义控件]了解模板化控件(5):VisualState
  16. sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)
  17. 给大家推荐一个好的android模拟器genymotion,非常流畅,跟真机差不多
  18. sql语句优化总结
  19. Reflector反编译.NET文件后修复
  20. BZOJ4057 [Cerc2012]Kingdoms

热门文章

  1. 项目用到了lua的哪些部分
  2. 【Spark学习】Apache Spark项目简介
  3. HW6.13
  4. HDU-4631 Sad Love Story 平面最近点对
  5. HDU-4611 Balls Rearrangement 循环节,模拟
  6. 说说QQ空间SEO
  7. 转载Jquery中的三种$()
  8. Java与MySql数据类型对照表
  9. [POLITICS] S Korea lawmakers vote to impeach leader
  10. sql存储过程exec执行字符串select 的区别