uniapp 防抖、节流、网络连接处理 、跳转传参

44 篇文章 1 订阅
40 篇文章 0 订阅

    

/**
 * @description 防抖
 * const debounceFn = debounce(function() { }[, 1000])
 * debounceFn(args)
 * or
 * handleFn: debounce(function() {this.fn()}, 1000)
 * @param {Function} fn
 * @param {Number} [delay] 500
 * @return {Function}
 */
export const debounce = (fn, delay = 500) => {
	let flag = true,
		timer = null;
	return function(...args) {
		if (flag) {
			fn.apply(this, args);
			flag = false;
		}
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(() => {
			clearTimeout(timer);
			flag = true;
		}, delay);
	};
};

/**
 * @description 节流
 * const throttleFn = throttle(function() { }[, 1000])
 * throttleFn(args)
 * or
 * handleFn: throttle(function() {this.fn()}, 1000)
 * @param {Function} fn
 * @param {Number} [delay] 500
 * @return {Function}
 */
export const throttle = (fn, delay = 500)=> {
	var timer = null;
	var startTime = Date.now();
	return function(...args) {
		var currentTime = Date.now();
		var remaining = delay - (currentTime - startTime);
		clearTimeout(timer);
		if (remaining <= 0) {
			fn.apply(this, args);
			startTime = Date.now();
		} else {
			timer = setTimeout(() => {
				fn.apply(this, args);
			}, remaining);
		}
	};
};

/**
 * @description 获取 URL 参数
 * getUrlParams("http://abc.com?id=1", "id")
 * OR
 * getUrlParams("http://abc.com?id=1").id
 * @param {String} url 普通链接
 * @param {String} [key] 需要获取的参数;不传返回所有参数(Object)
 * @return {String | Object}
 */
 export const getUrlParams = (url, key) => {
	const params = {};
	if (url) {
		const paramsStr = url.split("?")[1];
		if (paramsStr) {
			const paramsArr = paramsStr.split("&"),
				len = paramsArr.length;
			let i = 0;
			for (; i < len; i++) {
				let arr = paramsArr[i].split("=");
				if (arr[0] === key) {
					return arr[1];
				} else {
					params[arr[0]] = arr[1];
				}
			}
		}
	};
	return params;
}

/**
 * @description object 数据拼接为 URL 参数
 * const url ="/pages/index/index?" + buildUrlParams({ a: 1, b: 2})
 * @param {Object} options 
 * @return {String}
 */
export const buildUrlParams = (options) => {
	let parts = [];
	for (const [key, value] of Object.entries(options)) {
		if (key !== 'path') {
			parts.push(`${key}=${value}`);
		}
	}
	return parts.join("&");
}

/**
 * @description 版本比较
 * compareVersion("1.0.0", "2.0.0")
 * @param {Sting} v1
 * @param {String} v2
 * @return {Number} 0: v1 = v2 | 1: v1 > v2 | -1: v1 < v2
 */
export const compareVersion = (v1, v2) =>{
	v1 = v1.split(".");
	v2 = v2.split(".");
	const len = Math.max(v1.length, v2.length);
	while (v1.length < len) {
		v1.push("0");
	}
	while (v2.length < len) {
		v2.push("0");
	}
	for (let i = 0; i < len; i++) {
		const n1 = parseInt(v1[i]);
		const n2 = parseInt(v2[i]);
		if (n1 > n2) {
			return 1;
		} else if (n1 < n2) {
			return -1;
		}
	}
	return 0;
}

import { debounce,
    throttle,
    getUrlParams,
    buildUrlParams,
    compareVersion, } from "@/utils/utils.js"  引入所需的js

防抖例子
	// 领取前
			onReceiveBefore: debounce(async function() {
				if (!this.guiderData.id) {
					uni.showModal({
						title: "无法领取",
						content: "该店铺下还未有导购",
						showCancel: false,
					})
					return;
				};
				const isAttention = this.idata.isAttention === true ? await this.getWeChatWorkAttentionStatus() : true;
				if (!isAttention) {
					this.$emit("attention", this.idata)
					return;
				}
				uni.requestSubscribeMessage({
					tmplIds: [subscribeMessage.receiveSuccess],
					success: (res) => {
						console.log(res)
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {
						this.onReceive();
					}
				})
			}, 2000),
解码网络连接获取需要的参数
this.giftDetailId = getUrlParams(decodeURIComponent(this.options.q), "giftDetailId")

	const url = decodeURIComponent(options.q);
				const params = getUrlParams(url);
跳转连接传参
onToRobot() {
				uni.navigateTo({
					url: `/pages/robot/robot?${buildUrlParams(this.options)}`,
				})
			}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呱嗨喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值