Created
June 25, 2014 15:41
-
-
Save jikeytang/c05699ffd73d03bc2452 to your computer and use it in GitHub Desktop.
[ Javascript ] - 20140626-题目1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
用代码实现一个毫秒级的倒计时,且可以多处调用。 | |
格式如:离建群三周年还有: 30天-23小时-40分钟-58秒-32毫秒 | |
PS: | |
1. 回复时注意加上下面这句话,才会有语法高亮或格式缩进。 | |
```javascript | |
// you code | |
``` | |
2. 粘贴代码时请使用shift+tab,缩进前面的空白。 |
<div id="times"></div>
<br>
<br>
<div id="time2"></div>
<br>
<br>
<div id="time3"></div>
<br>
<br>
<div id="time4"></div>
<br>
<br>
<div id="time5"></div>
<script>
function fuckTime(id,str){
this.date=new Date(str).getTime();//new Date(2014,8,12,10,10,40);
this.times='';
this.o=document.getElementById(id);
this.speed();
}
fuckTime.prototype.speed=function(){
//alert(this.date+'*'+(new Date().getTime()));
var thisms = new Date().getTime();
var mms = this.date - thisms;
if(mms<=0){
this.o.innerHTML="正在进入开奖.....";
clearInterval(this.times);
return;
}
//alert(mms);
var dayfm = 3600 * 24 * 1000;//一天的毫秒
var d = Math.floor(mms / dayfm);
mms = mms - dayfm * d;
var h = Math.floor(mms/(3600*1000));
mms = mms - 3600 * 1000 * h;
var m = Math.floor(mms / 60000);
mms = mms - 60000 * m;
var s = Math.floor(mms / 1000);
mms = mms - 1000 * s;
var ms = mms;
var str = "开奖还有:" + d + "天—" + h + "小时—" + m + "分—" + s + "秒—" + ms;
this.o.innerHTML=str;
var that=this;
var doSpeed=function(){
return that.speed();
}
if(!this.times){
this.times=setInterval(doSpeed,1)
}
}
var a=new fuckTime("times","July 1,2014 12:12:12");
var b=new fuckTime("time2","August 9,2014 12:12:12");
var C=new fuckTime("time3","September 9,2014 12:12:12");
var D=new fuckTime("time4","October 9,2014 12:12:12");
var E=new fuckTime("time5","August 29,2014 12:12:12");
</script>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
菜鸟提几个问题,只是讨论,没有针对任何人的意思啊
1: settimeout setInterval 最短时间 1000 /60 = 16.7ms 用到这里【精准】判断毫秒时差 是否会有问题?
2 :