在开发小程序时遇到了需要做自定义倒计时的需求,比如番茄闹钟,比如安享智家的管家功能,这些都需要用到自定义倒计时组件,那么这个组件该如何开发呢?
下面可速云小编就带大家来看下自定义倒计时的组件怎么开发,也欢迎大家用微信小程序搜索下‘安享智家’,体验下小程序定时的功能
需求
- 可配置倒计时的时间
- 倒计时结束后执行事件
- 可配置倒计时时间的格式
步骤
- 先定义自定义组件的 properties ,这里有两个父组件传给该倒计时组件的参数 target 倒计时的时间, format 倒计时时间的格式
properties: {
target: {
type: String,
},
format: {
type: Function,
default: null
}
},
lifetimes: {
attached() {
this.setData({
lastTime:
this.initTime(
this.properties).lastTime, }, () => {
this.tick();
if (typeof
this.properties.format ===
'object') {
this.defaultFormat(
this.
data.lastTime)
}
})
},
detached() { clearTimeout(timer);
},
},
微信小程序自定义组件的生命周期指的是指的是组件自身的一些函数,这些函数在特殊的时间点或遇到一些特殊的框架事件时被自动触发。其中,最重要的生命周期是 created attached detached ,包含一个组件实例生命流程的最主要时间点。具体微信自定义组件学习参考官方文档
data: {
d:
0,
h:
0,
m:
0,
s:
0,
result:
'',
lastTime:
'' },
methods: { defaultFormat:
function(time) {
const day =
24 *
60 *
60 *
1000 const hours =
60 *
60 *
1000;
const minutes =
60 *
1000;
const d =
Math.floor(time / day);
const h =
Math.floor((time - d * day) / hours);
const m =
Math.floor((time - d * day - h * hours) / minutes);
const s =
Math.floor((time - d * day - h * hours - m * minutes) /
1000);
this.setData({
d,
h,
m,
s
})
}, tick:
function() {
let {
lastTime
} =
this.data;
timer = setTimeout(
() => {
if (lastTime < interval) {
clearTimeout(timer);
this.setData({
lastTime:
0,
result:
'' },
() => {
this.defaultFormat(lastTime)
if (
this.onEnd) {
this.onEnd();
}
}
);
}
else {
lastTime -= interval;
this.setData({
lastTime,
result:
this.properties.format ?
this.properties.format(lastTime) :
'' },
() => {
this.defaultFormat(lastTime)
this.tick();
}
);
}
}, interval);
}, initTime:
function(properties) {
let lastTime =
0;
let targetTime =
0;
try {
if (
Object.prototype.toString.call(properties.target) ===
'[object Date]') {
targetTime =
Number(properties.target).getTime();
}
else {
targetTime =
new Date(
Number(properties.target)).getTime();
}
}
catch (e) {
throw new Error(
'invalid target properties', e);
}
lastTime = targetTime -
new Date().getTime();
return {
lastTime: lastTime <
0 ?
0 : lastTime,
};
}, onEnd:
function() {
this.triggerEvent(
'onEnd');
}
}
defaultFormat :默认时间处理函数 tick :定时事件 initTime 初始化时间 onEnd :时间结束的回调
var timer =
0;
var interval =
1000;
Component({ properties: {
target: {
type:
String,
},
format: {
type:
Function,
default:
null }
},
lifetimes: {
attached() {
this.setData({
lastTime:
this.initTime(
this.properties).lastTime, }, () => {
this.tick();
if (
typeof this.properties.format ===
'object') {
this.defaultFormat(
this.data.lastTime)
}
})
},
detached() { clearTimeout(timer);
},
}, data: {
d:
0, h:
0, m:
0, s:
0, result:
'', lastTime:
'' }, methods: { defaultFormat:
function(time) {
const day =
24 *
60 *
60 *
1000 const hours =
60 *
60 *
1000;
const minutes =
60 *
1000;
const d =
Math.floor(time / day);
const h =
Math.floor((time - d * day) / hours);
const m =
Math.floor((time - d * day - h * hours) / minutes);
const s =
Math.floor((time - d * day - h * hours - m * minutes) /
1000);
this.setData({
d,
h,
m,
s
})
}, tick:
function() {
let {
lastTime
} =
this.data;
timer = setTimeout(
() => {
if (lastTime < interval) {
clearTimeout(timer);
this.setData({
lastTime:
0,
result:
'' },
() => {
this.defaultFormat(lastTime)
if (
this.onEnd) {
this.onEnd();
}
}
);
}
else {
lastTime -= interval;
this.setData({
lastTime,
result:
this.properties.format ?
this.properties.format(lastTime) :
'' },
() => {
this.defaultFormat(lastTime)
this.tick();
}
);
}
}, interval);
}, initTime:
function(properties) {
let lastTime =
0;
let targetTime =
0;
try {
if (
Object.prototype.toString.call(properties.target) ===
'[object Date]') {
targetTime =
Number(properties.target).getTime();
}
else {
targetTime =
new Date(
Number(properties.target)).getTime();
}
}
catch (e) {
throw new Error(
'invalid target properties', e);
}
lastTime = targetTime -
new Date().getTime();
return {
lastTime: lastTime <
0 ?
0 : lastTime,
};
}, onEnd:
function() {
this.triggerEvent(
'onEnd');
}
}
})
- 倒计时组件 countDown.wxml 完整代码
<wxs src="../wxs/utils.wxs" module="utils" /> <wxs src="../../comm.wxs" module="comm" /> <view class="count-down"> <text wx:if="{{result!==''}}">{{result}}text> <block wx:else> <text wx:if="{{comm.numberToFixed(d)>0}}">{{d}}天text> <text>{{utils.fixedZero(h)}}text> <text style="font-weight: 500">:
text>
<text>{{utils.fixedZero(m)}}text> <text style="font-weight: 500">:
text>
<text>{{utils.fixedZero(s)}}text> block>
view>
复制代码
其中引入了两个wxs文件中的函数
WXS(WeiXin Script)是小程序的一套脚本语言,结合 WXML,可以构建出页面的结构。官方文档
function fixedZero(val) {
return val *
1 <
10 ?
'0' + val : val;
}
function numberToFixed(number, pos) {
if (
number ===
null ||
number ===
'' ||
number <
0)
return '' return parseFloat(
number).toFixed(pos)
}
组件使用
"usingComponents": {
"countDown":
"../../../components/countDown/countDown" },
??<countDown bind:onEnd="getPageList" format="{{formatTime}}" target="{{creatTargetTime}}" const formatChinaDate =
mss => {
let days =
parseInt(mss / (
1000 *
60 *
60 *
24));
let hours =
parseInt((mss % (
1000 *
60 *
60 *
24)) / (
1000 *
60 *
60));
let minutes =
parseInt((mss % (
1000 *
60 *
60)) / (
1000 *
60));
let seconds =
parseInt((mss % (
1000 *
60)) /
1000);
return days +
' 天 ' + hours +
' 小时 ' + minutes +
' 分钟 ' + seconds +
' 秒 ';
};
data:{
formatTime:formatChinaDate,
creatTargetTime:
1556428889000, }
getPageList:
function(){
console.log(
'倒计时结束啦')
}
?以上就是微信小程序自定义倒计时组件的开发步骤,更多开发教程请关注“可速云”查阅
热门推荐: 上海微信小程序 小程序开发 小程序设计 支付宝小程序 百度小程序?
TAG标签:
小程序开发小程序组件
http://www.xiaochengxub.com/read-34742.html