js 倒计时插件(服务器时间,终端时间供选择)
来源:互联网 发布:javascript class 编辑:程序博客网 时间:2024/05/16 15:54
;(function(){ 'use strict'; var timer = function (params) { if (!(this instanceof timer)) return new timer(params); var defaults = { //结束时间 毫秒 endTimeMill: 0, //剩余时间 timeLeftMill: 0, //毫秒 speedMill: 1000, //服务器时间 serverTime:false, //延时时长毫秒 delayTimeMill:3000, //时间展示 showTime:undefined } var t = this; t.params = params || {}; for (var def in defaults) { if (typeof t.params[def] === 'undefined') { t.params[def] = defaults[def]; } } t.showTime = function(timeLeftMill){}; /** 定时任务开始 */ t.start = function(){ if (t.params.serverTime) { t.requestServerTime(); } else { t.params.timeLeftMill = t.params.endTimeMill - (new Date().getTime()); t.startCounting(); } }; //设置自定义值 t.requestServerTime = function(){ var xhr = null; if(window.XMLHttpRequest){ xhr = new window.XMLHttpRequest(); }else{ // ie xhr = new ActiveObject("Microsoft") } xhr.open("GET",location.href,true); xhr.send(null); xhr.onreadystatechange=function(){ if (xhr.status == 200) { if(xhr.readyState == 2){ var time = xhr.getResponseHeader("Date"); var serverTimeMill = new Date(time).getTime(); t.params.timeLeftMill = t.params.endTimeMill - serverTimeMill; t.startCounting(); } } else { t.params.timeLeftMill = t.params.endTimeMill - (new Date().getTime()); t.startCounting(); } } }; //绑定事件 t.startCounting = function(){ t.params.timeLeftMill -= t.params.speedMill; if(t.params.timeLeftMill > 0){ if(t.params.showTime) { t.params.showTime.call(this, t.params.timeLeftMill); } setTimeout(function(){ t.startCounting(); },t.params.speedMill); }else{ if(t.params.showTime) { t.params.showTime.call(this, 0); } } }; //提供API给外部使用 return { start:t.start,//设置值 }; }; window.timer = timer;}());
阅读全文
0 0
- js 倒计时插件(服务器时间,终端时间供选择)
- JS服务器获取时间倒计时
- JS根据服务器时间倒计时
- js获取本地时间、服务器时间、倒计时
- js比对服务器时间倒计时
- JS 活动倒计时 根据服务器时间判断
- js基于服务器时间的倒计时功能
- js基于服务器时间的倒计时功能
- 基于服务器时间的倒计时功能js
- js的倒计时 服务器时间与本地时间不一致
- 时间倒计时插件
- 时间倒计时 JS
- js时间倒计时
- JS 时间倒计时
- js时间倒计时
- JS 实现时间倒计时
- JS 时间倒计时
- js时间倒计时 (二)
- 虚拟机垃圾收集器与内存分配策略3.2
- 通讯录列表
- SQL Server 创建 MySQL 链接服务器
- 直接插入排序
- springMVC 点击超链接跳转没有反应原因
- js 倒计时插件(服务器时间,终端时间供选择)
- 获取apk中的MD5和SHA1
- 嵌入式学习历程【3】 Linux的shell编程
- Qt“__func__”: 未声明的标识符
- 移动端点击事件延迟 击透及解决
- java-InputStream中read()与read(byte[] b)
- 【Python开发】anaconda3 安装python包
- python里使用正则表达式的后向搜索否定模式
- nginx lua获取真实IP