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. 第63天:json的两种声明方式

    一. json 两种声明方式 1. 对象声明   var  json = {width:100,height:100} 2. 数组声明   var  man = [        //  数组的 js ...

  2. BZOJ 1040 骑士(环套树DP)

    如果m=n-1,显然这就是一个经典的树形dp. 现在是m=n,这是一个环套树森林,破掉这个环后,就成了一个树,那么这条破开的边连接的两个顶点不能同时选择.我们可以对这两个点进行两次树形DP根不选的情况 ...

  3. Andorid API Package ---> android.accessibilityservice

    包名: android.accessibilityservice                     Added in API level 4 URL:http://developer.andro ...

  4. HDU - 6333 Harvest of Apples

    题意: T次询问,每次给出n,m.求sigma(k:0->m)C(n, k). 题解: 用离线莫队来做. 令S(n,m) = sigma(k:0->m)C(n, k). S(n+1, m) ...

  5. 洛谷P3759 [TJOI2017]不勤劳的图书管理员 【树状数组套主席树】

    题目链接 洛谷P3759 题解 树状数组套主席树板题 #include<algorithm> #include<iostream> #include<cstring> ...

  6. POJ.1287 Networking (Prim)

    POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...

  7. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  8. UVA.129 Krypton Factor (搜索+暴力)

    UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...

  9. STL之五:set/multiset用法详解

    集合 转载于:http://blog.csdn.net/longshengguoji/article/details/8546286 使用set或multiset之前,必须加入头文件<set&g ...

  10. Codeforces Round #333 (Div. 2) B

    B. Approximating a Constant Range time limit per test 2 seconds memory limit per test 256 megabytes ...