倒计时

翻译 2006年06月08日 13:22:00
说明:使用方法:首先调用new cdtime("ID_of_DIV_container", "target_date")
  1. ID_of_DIV_container (string): 这是要显示内容的层的ID.
  2. target_date (string): 要到的时间日期格式,24小时制.

其它调用cdtime.displaycountdown("base_unit", formatfunction_reference)

  1. base_unit (string): 从顶到下的使用数目. 有效的参数是"days", "hours", "minutes", 或 "seconds." 如果你输入"hours",比如例子, 这个脚本将显示到指定的日期还剩多少 hours, minutes, 和 seconds .
  2. formatfunction_reference (string): 这是脚本中定义的函数名称.你可通过自定函数来设置显示的内容的样式和外观
以下代码加入<head>区域
<style style="text/css">
.lcdstyle{ /*Example CSS to create LCD countdown look*/
background-color:black;
color:yellow;
font: bold 18px MS Sans Serif;
padding: 3px;
}
.lcdstyle sup{ /*Example CSS to create LCD countdown look*/
font-size: 80%
}
</style>
<script type="text/javascript">
//more javascript from http://www.smallrain.net
function cdtime(container, targetdate){
if (!document.getElementById || !document.getElementById(container)) return
this.container=document.getElementById(container)
this.currentTime=new Date()
this.targetdate=new Date(targetdate)
this.timesup=false
this.updateTime()
}
cdtime.prototype.updateTime=function(){
var thisobj=this
this.currentTime.setSeconds(this.currentTime.getSeconds()+1)
setTimeout(function(){thisobj.updateTime()}, 1000) //update time every second
}
cdtime.prototype.displaycountdown=function(baseunit, functionref){
this.baseunit=baseunit
this.formatresults=functionref
this.showresults()
}
cdtime.prototype.showresults=function(){
var thisobj=this

var timediff=(this.targetdate-this.currentTime)/1000 //difference btw target date and current date, in seconds
if (timediff<0){ //if time is up
this.timesup=true
this.container.innerHTML=this.formatresults()
return
}
var oneMinute=60 //minute unit in seconds
var oneHour=60*60 //hour unit in seconds
var oneDay=60*60*24 //day unit in seconds
var dayfield=Math.floor(timediff/oneDay)
var hourfield=Math.floor((timediff-dayfield*oneDay)/oneHour)
var minutefield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour)/oneMinute)
var secondfield=Math.floor((timediff-dayfield*oneDay-hourfield*oneHour-minutefield*oneMinute))
if (this.baseunit=="hours"){ //if base unit is hours, set "hourfield" to be topmost level
hourfield=dayfield*24+hourfield
dayfield="n/a"
}
else if (this.baseunit=="minutes"){ //if base unit is minutes, set "minutefield" to be topmost level
minutefield=dayfield*24*60+hourfield*60+minutefield
dayfield=hourfield="n/a"
}
else if (this.baseunit=="seconds"){ //if base unit is seconds, set "secondfield" to be topmost level
var secondfield=timediff
dayfield=hourfield=minutefield="n/a"
}
this.container.innerHTML=this.formatresults(dayfield, hourfield, minutefield, secondfield)
setTimeout(function(){thisobj.showresults()}, 1000) //update results every second
}
/////CUSTOM FORMAT OUTPUT FUNCTIONS BELOW//////////////////////////////
//Create your own custom format function to pass into cdtime.displaycountdown()
//Use arguments[0] to access "Days" left
//Use arguments[1] to access "Hours" left
//Use arguments[2] to access "Minutes" left
//Use arguments[3] to access "Seconds" left
//The values of these arguments may change depending on the "baseunit" parameter of cdtime.displaycountdown()
//For example, if "baseunit" is set to "hours", arguments[0] becomes meaningless and contains "n/a"
//For example, if "baseunit" is set to "minutes", arguments[0] and arguments[1] become meaningless etc

function formatresults(){
if (this.timesup==false){//if target date/time not yet met
var displaystring=" 离2008年8月8日20:00:00北京奥运会还有:"+arguments[0]+" 天 "+arguments[1]+" 时 "+arguments[2]+" 分 "+arguments[3]+" 秒"
}
else{ //else if target date/time met
var displaystring="Future date is here!"
}
return displaystring
}
function formatresults2(){
if (this.timesup==false){ //if target date/time not yet met
var displaystring="离圣诞节还有:<span class='lcdstyle'>"+arguments[0]+" <sup>天</sup> "+arguments[1]+" <sup>时</sup> "+arguments[2]+" <sup>分</sup> "+arguments[3]+" <sup>秒</sup></span> "
}
else{ //else if target date/time met
var displaystring="" //Don't display any text
alert("Christmas is here!") //Instead, perform a custom alert
}
return displaystring
}
</script>
以下代码加入<body>区域
<div id="countdowncontainer"></div>
<br />
<div id="countdowncontainer2"></div>
<script type="text/javascript">
var futuredate=new cdtime("countdowncontainer", "August 8, 2008 20:00:00")
futuredate.displaycountdown("days", formatresults)
var currentyear=new Date().getFullYear()
//dynamically get this Christmas' year value. If Christmas already passed, then year=current year+1
var thischristmasyear=(new Date().getMonth()>=11 && new Date().getDate()>25)? currentyear+1 : currentyear
var christmas=new cdtime("countdowncontainer2", "December 25, "+thischristmasyear+" 0:0:00")
christmas.displaycountdown("days", formatresults2)
</script>

相关文章推荐

倒计时工具类,listview的item计时器

listview中的item,倒计时,设置倒计时时长及倒计时间隔

Android自定义控件之3D上下翻页效果的倒计时控件

这是一个自定义的倒计时控件,具有3D上下翻页翻转效果。最近项目中需要做一个倒计时控件,需要和iOS端的效果保持一样。大致效果是这样的,如下图所示: 由于暂时还不会怎么样制作gif动态图,所以想看具...

C++命令符式倒计时程序

要求:输入时间进行计时。

关于listview中添加倒计时功能卡顿问题

项目需要在listview中的每个item上加一个倒计时功能。listview就是普通的listview,倒计时也是用的android自带的倒计时功能,末尾会附带倒计时的显示最简单的textview自...

iOS开发-三种倒计时的写法

1.通过NSThread的performSelectorInBackground; 2.通过定时器,属于比较简单的写法; 3.通过GCD中的dispatch_source;先说第一种:#impor...

Android获取短信验证码按钮倒计时按钮

主要是用到CountDownTimer 类。   首先创建变量: public class RegisterActivity extends Activity{ private TimeC...
  • howlaa
  • howlaa
  • 2017年02月07日 11:40
  • 399

微信小程序开发(3)--倒计时60获取验证码

微信小程序获取验证码

PullToRefreshScrollView 嵌套RecyclerView实现特卖列表倒计时抢购

不久之前,我们谈到了通过Handler与timer及TimerTask结合实现倒计时抢购列表,那个是PullToRefreshListView实现的,今天要讲的是PullToRefreshScroll...

Vue2.0进阶组件篇4 突如其来(时间倒计时组件)

接下来的时候我想关注于node.js等一系前端知识,但是这个时间倒计时组件真的很突如其来,就在昨天,因为公司需求,要做一个倒计时,ok那没有问题,对于倒计时来说,一点都不难,肯定大家都写过,但是基于v...

Android 倒计时功能的实现(CountDownTimer)

 CountDownTimer 结构 继承关系 public abstract class CountDownTimer extends Object     java.lang.Ob...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:倒计时
举报原因:
原因补充:

(最多只允许输入30个字)