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. 完成! (日常记录)
相关推荐
灵感__idea1 小时前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
yinuo2 小时前
轻松接入大语言模型API -04
前端
袋鼠云数栈UED团队3 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
cipher3 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
UrbanJazzerati3 小时前
非常友好的Vue 3 生命周期详解
前端·面试
AAA阿giao3 小时前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
亦妤3 小时前
JS执行机制、作用域及作用域链
javascript
兆子龙4 小时前
像 React Hook 一样「自动触发」:用 Git Hook 拦住忘删的测试代码与其它翻车现场
前端·架构
兆子龙5 小时前
用 Auto.js 实现挂机脚本:从找图点击到循环自动化
前端·架构
SuperEugene5 小时前
表单最佳实践:从 v-model 到自定义表单组件(含校验)
前端·javascript·vue.js