关于uni-app 倒计时 清除、熄屏等问题

息屏:通过一行代码plus.device.setWakelock(true);来实现,设置让屏幕长亮。
倒计时:需要设置一个变量,通过setInterval函数来实时更新,注意:当返回上一页或者倒计时结束要 清除倒计时,否则会影响后面代码,已实测。

<template>
	<view>
			<text style="color: #E60000;">倒计时:{{interval}}</text>
	</view>
</template>	
<script>
  let maxtime = 0
	export default {
		data() {
			return {
				interval: '',
		        timer: null,
		        estimateTime: 0,
			}
		},
		onLoad(option) {
		      // 设置让屏幕长亮
		      plus.device.setWakelock(true);
		       maxtime = this.estimateTime * 60
		       // 很重要
		        this.timer = setInterval(() => {
		         	  this.countDown()
		      }, 1000)
		},	
		// 点击 返回 上一页
		onBackPress(e) {
		      if (e.from === 'backbutton' ) {
		        uni.showModal({
		          title: '提示',
		          content: '放弃本次考试,结束本次考试流程,是否结束?',
		          success: res => {
		            if (res.confirm) { 
		               // 清除倒计时
		              clearInterval(this.timer)
		              this.finishExam()
		            } else if (res.cancel) {
		            }
		          }
		        })
		        return true;
		      }
		 },
		methods: {
		  countDown: function () {
		    if (maxtime >= 0) {
			      let minutes = Math.floor(maxtime / 60) < 10 ? '0'+Math.floor(maxtime / 60) : Math.floor(maxtime / 60),
	              seconds = Math.floor(maxtime % 60) < 10 ? '0'+Math.floor(maxtime % 60) : Math.floor(maxtime % 60);
		          this.interval = `${minutes}:${seconds}`
		          maxtime = maxtime - 1;
	        } else {
			      clearInterval(this.timer)
		          uni.showModal({
		            title: '提示',
		            content: '倒计时结束!',
		            success: res => {
		              if (res.confirm) {
		                // 调接口
		                this.finishExam()
		              } else if (res.cancel) {
		                // 调接口
		                this.finishExam()
		              }
		            }
	          })
	       }   
        }
      },
</script>		
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是基于STM32F103的USART3的RS485通信代码,代码中包含了发送和接收数据的函数: ```c #include "stm32f10x.h" #include <stdio.h> void USART3_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //USART3_TX PB10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //USART3_RX PB11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); //USART3_RE PB12 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); } void USART3_SendData(uint8_t *pucBuffer, uint16_t uiLength) { GPIO_SetBits(GPIOB, GPIO_Pin_12); while (uiLength--) { USART_SendData(USART3, *pucBuffer++); while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET) { } } while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) { } GPIO_ResetBits(GPIOB, GPIO_Pin_12); } void USART3_ReceiveData(uint8_t *pucBuffer, uint16_t uiLength) { while (uiLength--) { while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET) { } *pucBuffer++ = USART_ReceiveData(USART3); } } ``` 在主函数中可以调用上述函数实现RS485通信: ```c int main(void) { uint8_t send_buffer[] = "Hello World!"; uint8_t receive_buffer[32]; USART3_Configuration(); while (1) { USART3_SendData(send_buffer, sizeof(send_buffer)); USART3_ReceiveData(receive_buffer, sizeof(receive_buffer)); } } ``` 在上述代码中,发送数据时需要先将RE置为高电平,然后通过USART3_SendData函数发送数据。接收数据时需要等待接收完成,然后通过USART3_ReceiveData函数将接收到的数据存储到缓冲区中。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值