javascript-倒计时-(咋个办呢-zgbn)
2016-02-03 10:29
543 查看
javascript-倒计时
下面的代码复制后直接可用<html> <script type="text/javascript"> /** * @parame eid 元素ID,将倒计时空间绑定在元素上 * @parame startDateTime 倒计时开始日期时间 countTime = {YY:年,MM:月,DD:日,HH:时,MS:分,SS:秒} * @parame unit 单位,按那个单位倒计时。例如'SS'按秒,'MS'按分;可选参数,默认为'SS' * @parame format 倒计时显示的样式:YYYY-MM-DD HH:MS:SS 或者 MS分:SS秒 可选参数,默认为 HH:MS:SS * return boolean */ function counDownTime(eid,startDateTime,unit,format) { if(!window._unit && !window._format && !window._eid && !window._count && !window._sumSS && !window.CDate){ if(eid && startDateTime){ window._unit = unit||'SS' ; window._format = format||'HH:MS:SS' ; window._eid = eid ; window._count = window._count||0 ; window._sumSS = 'SS'==window._unit?1000:'MS'==window._unit?60*1000:'HH'==window._unit?60*60*1000:'DD'==window._unit?24*60*60*1000:1000 ; window.CDate = new Date(startDateTime.YY,startDateTime.MM-1,startDateTime.DD,startDateTime.HH,startDateTime.MS,startDateTime.SS) ; } else{ return false ; } } window._count = window._count+window._sumSS-0 ; var nowDate = new Date() ; var setEshow = function(date){ var stime = window._format ; if(stime.indexOf('YYYY')!=-1){ stime = stime.replace('YYYY', date?date.getFullYear():0) ; } if(stime.indexOf('MM')!=-1){ stime = stime.replace('MM', date?date.getMonth()+1:0) } if(stime.indexOf('DD')!=-1){ stime = stime.replace('DD', date?date.getDate():0) } if(stime.indexOf('HH')!=-1){ stime = stime.replace('HH', date?date.getHours():0) } if(stime.indexOf('MS')!=-1){ stime = stime.replace('MS', date?date.getMinutes():0) } if(stime.indexOf('SS')!=-1){ stime = stime.replace('SS', date?date.getSeconds():0) } return stime ; } ; var destructor = function(){ window._unit = null ; window._format = null ; window._eid = null ; window._count = null ; window._sumSS = null ; window.CDate = null ; } if(nowDate.getTime() >= window.CDate.getTime()){ document.getElementById(window._eid).innerHTML = setEshow(null); destructor() ; return true ; } else{ document.getElementById(window._eid).innerHTML = setEshow(new Date(window.CDate.getTime()-window._count)); } setTimeout("counDownTime()",window._sumSS) ; } </script> <body> <h1>倒计时</h1> <span id='time' style='font-size:40px;color:#F00'>05.00</span><span id='time' style='font-size:40px;color:#F00'>s</span> <input type=button value='倒计时' onclick='counDownTime("time",{YY:2011,MM:7,DD:28,HH:15,MS:0,SS:0},null)' /> </body> </html>
相关文章推荐
- JavaScript获取当前运行脚本文件所在目录的方法
- JavaScript获取对象在页面中位置坐标的方法
- JSP
- Javascript函数声明和函数表达式
- Video of Nanjing receives millions of hits
- js密码强度验证代码
- QScript 解析json
- JSONP跨域的原理解析及其实现介绍
- Javascript模式——函数提升 (笔记)
- ExtJS 上传下载控件
- jsp 通过流的方式读取图片
- 将JSON转换为C#的类
- JavaScript高级程序设计学习笔记第一章
- javascript点击按钮实现隐藏显示切换效果
- JavaScript检查子字符串是否在字符串中的方法
- Javascript的闭包
- JS实现CheckBox复选框全选、不选或全不选功能
- javascript实现在网页中运行本地程序的方法
- javascript document.compatMode
- 简单实现js选项卡切换效果