JavaScript

创建一个 countdown 方法,用于计算并在控制台打印距目标时间的日、时、分、秒数,每隔一秒递归执行一次。

msec 是当前时间距目标时间的毫秒数,由时间戳相减得到,我们将以这个数为基础计算。我们都知道1天等于24小时,1小时等于60分钟,1分钟等于60秒,1秒等于1000毫秒。所以,msec / 1000 / 60 / 60 / 24 保留整数就是天数。如果换用 % 取余数,再保留整数后得到的就是小时数。以此类推就能算出其他所有数。

function countdown () {
// 目标日期时间戳
const end = Date.parse(new Date('2017-12-01'))
// 当前时间戳
const now = Date.parse(new Date())
// 相差的毫秒数
const msec = end - now
// 计算时分秒数
let day = parseInt(msec / 1000 / 60 / 60 / 24)
let hr = parseInt(msec / 1000 / 60 / 60 % 24)
let min = parseInt(msec / 1000 / 60 % 60)
let sec = parseInt(msec / 1000 % 60)
// 个位数前补零
hr = hr > 9 ? hr : '0' + hr
min = min > 9 ? min : '0' + min
sec = sec > 9 ? sec : '0' + sec
// 控制台打印
console.log(`${day}天 ${hr}小时 ${min}分钟 ${sec}秒`)
// 一秒后递归
setTimeout(function () {
countdown()
}, 1000)
}

控制台打印结果:

27天 07小时 49分钟 10秒
27天 07小时 49分钟 09秒
27天 07小时 49分钟 08秒
...

Vue.js

如果单纯使用 JavaScript ,我们需要在每次计算后手动更新 DOM 元素(将数据显示给用户),既不方便又难以维护。实际项目中更多的是配合前端框架,将计算结果实时渲染到页面上。

页面结构中的数据来自 Vue 实例的 data 对象。

<div id="app">{{ `${day}天 ${hr}小时 ${min}分钟 ${sec}分钟` }}</div>

mounted 是 Vue 的生命周期方法,可以理解为在页面加载完毕后执行 countdown 方法。countdown 方法每隔一秒会执行一次,并将计算结果分别赋予变量 dayhrminsec,同时改变的还有页面上显示的内容。

new Vue({
el: '#app',
data: function () {
return {
day: 0, hr: 0, min: 0, sec: 0
}
},
mounted: function () {
this.countdown()
},
methods: {
countdown: function () {
const end = Date.parse(new Date('2017-12-01'))
const now = Date.parse(new Date())
const msec = end - now
let day = parseInt(msec / 1000 / 60 / 60 / 24)
let hr = parseInt(msec / 1000 / 60 / 60 % 24)
let min = parseInt(msec / 1000 / 60 % 60)
let sec = parseInt(msec / 1000 % 60)
this.day = day
this.hr = hr > 9 ? hr : '0' + hr
this.min = min > 9 ? min : '0' + min
this.sec = sec > 9 ? sec : '0' + sec
const that = this
setTimeout(function () {
that.countdown()
}, 1000)
}
}
})

上述摘自网上 ,对于vue的写法我个人有一个优化,当我们离开吗,某个界面时不应该让他在计时,而是我们在当前页面的时候才计时这样显得更合理一点

<template>
<span v-if="Date.parse(new Date(deadline)) <= Date.parse(new Date()) && Date.parse(new Date()) < Date.parse(new Date(start))">待开始</span>
<span v-else-if="Date.parse(new Date())===Date.parse(new Date(start))">已开始</span>
<span v-else>倒计时{{`${day}天${hr}小时${min}分钟${sec}秒`}}</span>
</template> <script>
export default {
props: ['deadline', 'start'],
name: "dateComponent",
data: function () {
return {
day: 0, hr: 0, min: 0, sec: 0,
}
},
mounted: function () {
this._interval = setInterval(() => {
this.countdown();
}, 1000)
},
destroyed () {
clearInterval(this._interval)
},
methods: {
countdown: function () {
const end = Date.parse(new Date(this.start));
const now = Date.parse(new Date());
const msec = end - now;
let day = parseInt(msec / 1000 / 60 / 60 / 24);
let hr = parseInt(msec / 1000 / 60 / 60 % 24);
let min = parseInt(msec / 1000 / 60 % 60);
let sec = parseInt(msec / 1000 % 60);
this.day = day;
this.hr = hr > 9 ? hr : '0' + hr;
this.min = min > 9 ? min : '0' + min;
this.sec = sec > 9 ? sec : '0' + sec; // const that = this;
// console.log(this.day===0 && this.hr==='00' && this.min==='00'&& this.sec==='00');
// if(this.day===0 && this.hr=== '00' && this.min==='00'&& this.sec ==='00'){
// console.log(1234566)
// return
// }
// setTimeout(function () {
// that.countdown()
// }, 1000)
} }, }
</script> <style scoped> </style>

vue-实现倒计时功能的更多相关文章

  1. html5 canvas 实现倒计时 功能

    function showTime(a) { var b = { id: "showtime", //canvasid x: 60, //中心点坐标 X轴; y: 60, //中心 ...

  2. 模块:js实现一个倒计时功能

    1.给显示内容加样式 <style> #p1{font-size: large; color: red;} </style> 2.客户端页面 <div id=" ...

  3. iOS “获取验证码”按钮的倒计时功能

    iOS 的倒计时有多种实现细节,Cocoa Touch 为我们提供了 NSTimer 类和 GCD 的dispatch_source_set_timer方法去更加方便的使用计时器.我们也可以很容易的的 ...

  4. Android 关于倒计时功能的实现

    关于倒计时的实现,可以说有很多的方法,比较常见的就是Timer+TimerTask+Handler了,或者还可以配合Runnable.例如下面的代码: import java.util.Timer; ...

  5. iOS 按钮倒计时功能

    iOS 按钮倒计时功能, 建议把按钮换成label,这样会避免读秒时闪烁 __block ; __block UIButton *verifybutton = _GetverificationBtn; ...

  6. js实现是倒计时功能

    工作中经常用到倒计时的功能,最近在整理之前做的项目的时候,发现自己写过一个倒计时的功能的效果,这里和大家分享下!实现这个功能是用原生js写的,不需要加载额外的库文件!功能比较简单,但是可以在此基础上扩 ...

  7. App启动页倒计时功能

    转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6753418.html 示例代码采用 RxJava + RxLifecycle + Data-Binding ...

  8. Vue.js 基本功能了解

    一.写在前面 隔了这么久才来出Vue的第二篇文章,真是堕落了,自己先惩罚下/(ㄒoㄒ)/~~ 回过头看自己第一篇相关文章<初试 Vue.js>(http://www.cnblogs.com ...

  9. JS实现为控件添加倒计时功能

    一.概述 在有些报表需求中,需要为控件添加倒计时功能,限制到某一个时间点后能进行一项操作或不能进行某项操作,比如查询,导出功能等等,又需要人性化地显示还有多少时间,即倒计时功能,比如下图中我们限制这个 ...

  10. 原生js实现一个简单的倒计时功能

    大家好,我是云中君!欢迎大家来观看我的博客 之前那,在群里看到很多人问,关于电商网站中的倒计时功能怎么实现,很多人说在网上找了很多插件,但是不是很会用,所以今天就在这里分享一下我封装的一个小的倒计时功 ...

随机推荐

  1. PL/SQL在 win8.1系统下连接Oracle11g没有database处理方法(亲身实验,吐血分享)

    一.问题 这里首先说明下我的环境:win8.1(64bit)+oracle11g(64bit)+PL/SQL(32bit).状况是:net manager正常配置,测试也成功,但是用PL/SQL连接的 ...

  2. 苹果ATS特性服务器配置指南 HTTPS 安卓可以用 IOS 报错。

    解决方案:https://www.qcloud.com/document/product/400/6973 ATS检测:https://www.qcloud.com/product/ssl#userD ...

  3. BZOJ5466 NOIP2018保卫王国(倍增+树形dp)

    暴力dp非常显然,设f[i][0/1]表示i号点不选/选时i子树内的答案,则f[i][0]=Σf[son][1],f[i][1]=a[i]+Σmin(f[son][0],f[son][1]). 注意到 ...

  4. CF995A Tesla

    题目描述 Allen dreams of one day owning a enormous fleet of electric cars, the car of the future! He kno ...

  5. 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出

    安装微软的windows补丁 KB2781514 ,补丁主要解决“在 .NET Framework 4.5 更新之后,Visual Studio 用户可能无法打开或创建 C++ 或 JavaScrip ...

  6. 【CKplayer】使用CKplayer插件在网页中嵌入视频的方法

    在做网站中有时候我们需要在网页中嵌入视频,一般视频嵌入有以下几种方法: 1. 优酷代码嵌入 优点:简单,方便,可靠. 缺点:有广告,现在的网站非常注重用户体验,如果打开一个在线视频是有长广告的一定会崩 ...

  7. 洛谷 P3620 [APIO/CTSC 2007]数据备份 解题报告

    P3620 [APIO/CTSC 2007]数据备份 题目描述 你在一家 IT 公司为大型写字楼或办公楼(offices)的计算机数据做备份.然而数据备份的工作是枯燥乏味的,因此你想设计一个系统让不同 ...

  8. Vue报错

    Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 8.x Found ...

  9. Delight for a Cat

    Time Limit: 1000 ms Memory Limit: 512 MB Description ​ 从前,有一只懒猫叫CJB.每个小时,这只猫要么在睡觉,要么在吃东西,但不能一边睡觉一边吃东 ...

  10. 内存和cpu

    http://www.blogjava.net/fjzag/articles/317773.html ubuntu@ubuntu-vm:/work/sv-g5-application/projects ...