当前位置:网站首页>uniapp组件之倒计时(如阅读协议倒计时、完成学习倒计时)
uniapp组件之倒计时(如阅读协议倒计时、完成学习倒计时)
2022-07-29 05:10:00 【薇森】
一、最后实现效果
二、倒计时的实现
计时器的主要代码如下:
// 倒计时事件(times为自己设置的时间,这里是10)
countDownTime:function(){
var that = this;
if(that.times == 10){
that.sid = setInterval(function() {
that.times--;
if(that.times == 0){
//时间到了清除计时器
clearInterval(that.sid);
}
}, 1000);
}
},
注意:在计时器执行倒计时的过程中,用户可能会退出该页面,此时需要在退出时清除计时器, 这里需要用到onUnload(),如下:
// 返回到上一页,清除定时器,防止离开此页面后,计时器还在执行
onUnload() {
clearInterval(this.sid);
},
三、倒计时与已学习显示切换
通过设置两个布尔变量(timeshow、learned),通过v-show来控制其显示与不显示,倒计时中显示倒计时,结束后显示已学习。
前端部分:
<!-- 学习倒计时完成 -->
<view v-show="timeshow" class="study-status-time">{
{times}}s</view>
<!-- 显示已学习 -->
<view v-show="learned" class="study-status">已学习</view>
js部分:
// 倒计时事件
countDownTime:function(){
var that = this;
if(that.times == 10){
that.sid = setInterval(function() {
that.times--;
if(that.times == 0){
clearInterval(that.sid);
that.timeshow=false;
that.learned=true;
// 提示框弹出
uni.showToast({
title:'文章已学习',
icon:'none',
duration:2000
})
}
}, 1000);
}
},
四、完整代码
<template>
<view class="study-content">
<!-- 文章标题 -->
<view class="article-index-title">文章标题</view>
<!-- 文章内容 -->
<view class="article-content"> 文章内容</view>
<!-- 学习完成以及体会填写 -->
<view class="content-bottom">
<view class="content-bootom-item">
<!-- 学习体会填写 -->
<view class="idea-input" @click="inputideal()" ><span>填写学习体会</span></view>
<!-- 学习倒计时完成 -->
<view v-show="timeshow" class="study-status-time">{
{times}}s</view>
<!-- 显示已学习 -->
<view v-show="learned" class="study-status">已学习</view>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
times:10,
timeshow:true,
learned:false,
}
},
// 页面一开始就执行倒计时方法
onLoad() {
this.countDownTime()
},
// 返回到上一页,清除定时器,防止离开此页面后,计时器还在执行
onUnload() {
clearInterval(this.sid);
},
methods: {
// 倒计时事件
countDownTime:function(){
var that = this;
if(that.times == 10){
that.sid = setInterval(function() {
that.times--;
if(that.times == 0){
clearInterval(that.sid);
that.timeshow=false;
that.learned=true;
// 提示框弹出
uni.showToast({
title:'文章已学习',
icon:'none',
duration:2000
})
}
}, 1000);
}
},
// 点击输入体会框,若未完成则提示还有几秒,已完成则可输入
inputideal:function(){
if(this.times!=0){
uni.showToast({
title:'还有'+this.times+'秒可填写',
icon:'none',
duration:2000
})
return
}
}
}
}
</script>
<style>
.study-content{
width: 100%;
height: 100%;
}
.article-index-title{
padding: 30rpx 40rpx 20rpx 40rpx;
font-size: 40rpx;
font-weight: 600;
text-align: justify;
}
.article-content{
box-sizing: border-box;
padding-left: 40rpx;
padding-right: 40rpx;
overflow: hidden;
font-size: 32rpx;
width: 100%;
text-align: justify;
line-height: 1.8;
padding-bottom: 50rpx;
}
.content-bottom{
height: 120rpx;
width: 100%;
position: fixed;
bottom: 0;
border-top: 1px #f0f0f0 solid;
background-color: #ffffff;
z-index: 100;
}
.content-bootom-item{
margin-left: 20rpx;
margin-right: 20rpx;
display: flex;
justify-content: space-between;
}
.idea-input{
width: 75%;
background-color: #f0f0f0;
height: 80rpx;
margin-top: 20rpx;
border-radius: 20rpx;
display: flex;
align-items: center;
}
.idea-input span{
color: #8b8b8b;
margin-left: 20rpx;
}
.study-status{
width: 20%;
background-color: #0055ff;
height: 80rpx;
margin-top: 20rpx;
border-radius: 20rpx;
display: flex;
align-items: center;
justify-content: center;
color: #ffffff;
}
.study-status-time{
width: 20%;
background-color: #8f8f8f;
height: 80rpx;
margin-top: 20rpx;
border-radius: 20rpx;
display: flex;
align-items: center;
justify-content: center;
color: #ffffff;
}
</style>
边栏推荐
- Side effects and sequence points
- Realize simple database query (incomplete)
- 167. Sum of two numbers II - enter an ordered array
- Differences between texture2d and texture2dproj under webgl1.0
- 365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
- ClickHouse学习(二)ClickHouse单机安装
- Do students in the science class really understand the future career planning?
- 第三课threejs全景预览房间案例
- 365 day challenge leetcode 1000 questions - day 040 design jump table + avoid flooding + find the latest grouping with size M + color ball with reduced sales value
- Li Kou 994: rotten orange (BFS)
猜你喜欢
实现简单的数据库查询(不完整)
C语言 一级指针
Clickhouse learning (VIII) materialized view
365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
ClickHouse学习(九)clickhouse整合mysql
【C语言系列】— 不创造第三个变量,实现两个数的交换
ClickHouse学习(八)物化视图
365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election
On Paradigm
Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens
随机推荐
省市区三级联动(简单又完美)
组件传参与生命周期
Longest string without duplicate characters
阿里云张新涛:异构计算为数字经济提供澎湃动力
存储类别
With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
数据库操作 Day 6
抢先预约 | 阿里云无影云应用线上发布会预约开启
D3d Shader Instruction
题解:在一个排序数组中查找元素第一个和最后一个的位置 (个人笔记)
【无标题】
B - identify floating point constant problems
[C language series] - print prime numbers between 100 and 200
ClickHouse学习(四)SQL操作
Clickhouse learning (XI) clickhouseapi operation
B - 识别浮点常量问题
用threejs 技术做游戏跑酷
关于局部变量
重定向和文件
Clickhouse learning (IV) SQL operation