js倒计时代码实现
来源:互联网 发布:黑暗之魂3优化 编辑:程序博客网 时间:2024/06/11 14:33
/** * *返回剩余时间 {} */function getTimeInfo(startTime, currentTime) { var total = startTime.getTime() - currentTime.getTime(); return { day: Math.floor(total/1000/60/60/24), //天 hour: Math.floor(total/1000/60/60%24), //时 minute: Math.floor(total/1000/60%60), //分 second: Math.floor(total/1000%60) //秒 }}var firstLoadTime = true;//动态倒计时处理// startTime 开始时间 *// ajaxUrl $ ajaxUrl 存在时执行 不存在时或ajax出错时默认以本地时间为准// attr keyfunction executeShow(startTime, ajaxUrl, attr, log) { var currentTime = new Date(); if (!firstLoadTime) { //非首次获取时间 if(window.currentTime != null) { currentTime = new Date(window.currentTime.getTime() + 1000); } } else { try { if (ajaxUrl != null && ajaxUrl.trim() != "") { //通过ajax获取 var ajaxtime = $.ajax({ url: ajaxUrl, async: false, dataType: "json", timeout: 1500, success: function(data) { try { if (!attr) { attr = "date"; } var date = data[attr]; //获取输出的服务器时间 date = date.replace(/-/g, "/"); currentTime = new Date(date); if (log && console) { console.log("通过jquery初始化服务端时间成功"); } } catch (e) { currentTime = new Date(); } }, complete: function(XMLHttpRequest, status) { //请求完成后最终执行参数 if (status == 'timeout') { //超时 ajaxtime.abort(); //取消请求 currentTime = new Date(); } else if (status == 'error') { currentTime = new Date(); } } }); } } catch(e) { currentTime = new Date(); } firstLoadTime = false; } window.currentTime = currentTime; setShowView(getTimeInfo(startTime, currentTime)); //设置页面显示的内容 setTimeout(function() { executeShow(startTime, ajaxUrl, attr, log); }, 1000);}//设置网页显示内容的函数,timeInfo为剩余时间jsonfunction setShowView(timeInfo) { console.log(timeInfo);}window.onload = function() { var startTime = new Date("2017/11/11 00:00:00"); //设置活动开始的时间 executeShow(startTime);}
阅读全文
0 0
- js倒计时代码实现
- JS 倒计时实现代码(时、分,秒)
- Html js实现倒计时效果参考代码
- 有关倒计时JS代码
- JS倒计时代码
- JS倒计时代码
- js倒计时代码
- js倒计时代码
- JS倒计时代码
- JS倒计时代码
- JS 倒计时代码
- js倒计时代码
- JS倒计时代码
- js倒计时代码
- JS----倒计时代码
- JS倒计时代码
- JS倒计时代码
- JS倒计时代码
- 面试宝典(PART VI) 各种各样的指针
- Redis、Memcache和MongoDB的区别
- mysql ==SET FOREIGN_KEY_CHECKS==用法
- 2017.10.16 利用Xshell登录学校服务器
- ovirt4 The redirection URI for client is not registered
- js倒计时代码实现
- 微博登录及删除粉丝/转发/评论的脚本[更新中]
- 弹性与性能兼俱_阿里云神龙云服务器全解析
- JavaScript中操作Mysql数据库实例
- MySQL中使用表别名与字段别名的基本教程
- JBoss 7.1.1启动时遇到Address already in use: bind /127.0.0.1:9990的处理办法
- 利用 HashSet不重复特性返回素组中重复元素
- react map遍历
- 【spring】关于location路径的设置(相对路径和绝对路径)