uniapp H5 touchstart执行完成并返回成功再执行touchend 失败则不执行touchend

uniapp H5 touchstart执行完成并返回成功再执行touchend 失败则不执行touchend

  1. 直接上代码
javascript 复制代码
<tmmplate>
    // 只绑定 touchstart 事件
	<view class="mp_yun_item">
		<view class="mp-ptz-btn mp-yun-top" @touchstart="yunControl">
		</view>
		<view class="mp-ptz-btn mp-yun-right" @touchstart="yunControl">
		</view>
		<view class="mp-ptz-btn mp-yun-bottom" @touchstart="yunControl">
		</view>
		<view class="mp-ptz-btn mp-yun-left" @touchstart="yunControl">
		</view>
	</view>
</template>

<script>
export default {
  data (){
     return {
       robotId:"",
       isfirst:true,
       loadingHide:false
     }
  },
  methods:{
  	//云台控制开始
	yunControl() {
		let that = this;
		// 第一个方法的代码
		let params = {
			robotId: this.robotId,
			action: 'move',
			action_params: {},
		}
          // 声明 p 接受一个promise 异步函数 
		let p = new Promise((resolve, reject) => {
		    // 此处调用 touchstart 触发的接口
			FridApi.setPtzAction(params)
				.then((result) => {
					const res = result
					if (res && res.code === 0) {
					    this.isfirst = true
					    // 成功后抛出
						resolve();
					} else {
						this.$refs.uToast.show({
							message: res?.msg || "云台操作失败",
							duration: 1000,
						})
					}
				})
				.finally(() => {
					this.loading = false
				})
		});
		 // 监听 touchend 鼠标抬起事件 
		window.addEventListener("touchend", (event) => {
		    // p 函数的成功回调
			p.then(value => {
			   // 在 p 函数的 成功回调中再调用  touchend 鼠标抬起事件 
				this.yunControlEnd(params)
			})
		}, {
			once: true
		});
	},
	//云台控制结束  该方法为抬起事件的 执行接口
	yunControlEnd(params) {
		this.loadingHide = true
		// 调用接口
		FridApi.setPtzAction(params)
			.then((result) => {
				const res = result
				this.loadingHide = false
				if (res && res.code === 0) {

				} else {
					this.$refs.uToast.show({
						message: res?.msg || "云台操作失败",
						duration: 1000,
					})
					// 如果抬起事件执行失败 就再执行一次
					if (this.isfirst) {
						this.isfirst = false;
						this.yunControlEnd(params)
					}

				}
			})
			.finally(() => {
				this.loadingHide = false
			})
	},
  }
}
</script>
  1. 完成! (日常记录)
相关推荐
0思必得024 分钟前
[Web自动化] Selenium处理滚动条
前端·爬虫·python·selenium·自动化
Misnice27 分钟前
Webpack、Vite、Rsbuild区别
前端·webpack·node.js
青茶36028 分钟前
php怎么实现订单接口状态轮询(二)
前端·php·接口
大橙子额1 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
WooaiJava2 小时前
AI 智能助手项目面试技术要点总结(前端部分)
javascript·大模型·html5
爱喝白开水a3 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
Never_Satisfied3 小时前
在JavaScript / HTML中,关于querySelectorAll方法
开发语言·javascript·html
董世昌413 小时前
深度解析ES6 Set与Map:相同点、核心差异及实战选型
前端·javascript·es6
WeiXiao_Hyy3 小时前
成为 Top 1% 的工程师
java·开发语言·javascript·经验分享·后端
吃杠碰小鸡4 小时前
高中数学-数列-导数证明
前端·数学·算法