小程序实现正计时和倒计时

小程序实现正计时和倒计时

1、正计时:

实现后的样式:

代码:

var timer; // 计时器

Page({

data: {

seconds: 0,

secondMax: 3599,

timeMax:'60:00',

maxTime:'60:00',

actionMax:true,

stopMaxTime:false,

resetMax:false,

time: '00:00',

cost: 0,

action:true,

stopTime:false,

reset:false,

childName: [

{ "name": '选项1', "active": true },

{ "name": '选项2', "active": false },

{ "name": "选项3", "active": false },

{ "name": '选项4', "active": false }

],

active1:false,

active2: true,

active3: false,

active4: true,

active: 'Exercise-Program-choose-one'

},

cilckChild(e) {

const that = this;

let index = e.currentTarget.dataset.index;

let childName = that.data.childName;

for (let i = 0; i < childName.length; i++) {

if (index == i) {

childName[i].active = true;

} else {

childName[i].active = false;

}

}

that.setData({ childName: childName })

},

onLoad: function (options) {},

timing(e){

this.setData({ action: !this.data.action, stopTime:false})

timing(this)

},

stopTime(e) {

console.log("现在的记录时间时长:" + this.data.seconds)

this.setData({ stopTime: true, reset: true })

},

reset(e) {

const that = this;

if (!that.data.reset) {

that.setData({ stopTime: true })

} else {

that.setData({ seconds: 0, action: !that.data.action })

}

},

timingMax(e){

this.setData({ actionMax: !this.data.actionMax, stopMaxTime: false })

timingMax(this)

},

stopMaxTime(e){

console.log("现在是记录的时间时长:",this.data.secondMax)

this.setData({ stopMaxTime: true, resetMax: true })

},

resetMax(e) {

const that = this;

if (!that.data.resetMax) {

that.setData({ stopMaxTime: true })

} else {

that.setData({ secondMax: 3600, actionMax: !that.data.actionMax })

}

},

//正计时

function timing(that) {

var seconds = that.data.seconds

if (seconds > 3600) {

that.setData({

time: '60:00'

});

return;

}

if (that.data.stopTime){

if(!that.data.reset){

that.setData({ seconds: 0, action: !that.data.action })

return;

}

return;

}

setTimeout(function () {

that.setData({

seconds: seconds + 1

});

timing(that);

}, 1000)

formatSeconds(that)

}

function formatSeconds(that) {

var mins = 0, hours = 0, seconds = that.data.seconds, time = ''

if (seconds < 60) {



} else if (seconds < 3600) {

mins = parseInt(seconds / 60)

seconds = seconds % 60

} else {

mins = parseInt(seconds / 60)

seconds = seconds % 60

hours = parseInt(mins / 60)

mins = mins % 60

}

that.setData({

// time: formatTime(hours) + ':' + formatTime(mins) + ':' + formatTime(seconds)

time: formatTime(mins) + ':' + formatTime(seconds)

});

}

function charging(that) {

if (that.data.seconds < 600) {

that.setData({cost : 1})

}

}

2、倒计时:

实现后的设计图:

代码片段:

var timer; // 计时器

Page({

data: {

seconds: 0,

secondMax: 3599,

timeMax:'60:00',

maxTime:'60:00',

actionMax:true,

stopMaxTime:false,

resetMax:false,

time: '00:00',

cost: 0,

action:true,

stopTime:false,

reset:false,

childName: [

{ "name": '选项1', "active": true },

{ "name": '选项2', "active": false },

{ "name": "选项3", "active": false },

{ "name": '选项4', "active": false }

],

active1:false,

active2: true,

active3: false,

active4: true,

active: 'Exercise-Program-choose-one'

},

cilckChild(e) {

const that = this;

let index = e.currentTarget.dataset.index;

let childName = that.data.childName;

for (let i = 0; i < childName.length; i++) {

if (index == i) {

childName[i].active = true;

} else {

childName[i].active = false;

}

}

that.setData({ childName: childName })

},

onLoad: function (options) {},

timing(e){

this.setData({ action: !this.data.action, stopTime:false})

timing(this)

},

stopTime(e) {

console.log("现在的记录时间时长:" + this.data.seconds)

this.setData({ stopTime: true, reset: true })

},

reset(e) {

const that = this;

if (!that.data.reset) {

that.setData({ stopTime: true })

} else {

that.setData({ seconds: 0, action: !that.data.action })

}

},

timingMax(e){

this.setData({ actionMax: !this.data.actionMax, stopMaxTime: false })

timingMax(this)

},

stopMaxTime(e){

console.log("现在是记录的时间时长:",this.data.secondMax)

this.setData({ stopMaxTime: true, resetMax: true })

},

resetMax(e) {

const that = this;

if (!that.data.resetMax) {

that.setData({ stopMaxTime: true })

} else {

that.setData({ secondMax: 3600, actionMax: !that.data.actionMax })

}

},
function timingMax(that) {

var seconds = that.data.secondMax

if (seconds <= 0) {

that.setData({

timeMax: '00:00'

});

return;

}

if (that.data.stopMaxTime) {

if (!that.data.resetMax) {

that.setData({ secondMax: 3600, actionMax: !that.data.actionMax })

return;

}

return;

}

setTimeout(function () {

that.setData({

secondMax: seconds - 1

});

timingMax(that);

}, 1000)

formatSecondsMax(that)

}

function formatSecondsMax(that) {

var mins = 60, hours = 1, seconds = that.data.secondMax, timeMax = ''

if (seconds < 60) {

mins = parseInt(mins%60)

} else if (seconds <= 3600) {

mins = parseInt(seconds / 60)

seconds = seconds % 60

} else {

mins = parseInt(seconds / 60)

seconds = seconds % 60

hours = parseInt(mins / 60)

mins = mins % 60

}

console.log(formatTime(mins))

console.log(formatTime(seconds))

that.setData({

// time: formatTime(hours) + ':' + formatTime(mins) + ':' + formatTime(seconds)

timeMax: formatTime(mins) + ':' + formatTime(seconds)

});

}

function formatTime(num) {

if (num < 10)

return '0' + num

else

return num + ''

}

 

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值