vue保留n位小数+防抖+节流,函数封装

位置@/utils/publicMethods.js
/*
 * 保留n位小数,默认2位
 * 658.146  => 658.14
 */
export const retainDecimals = (number, n = 2) => {
    if (isNumber(number)) {
     number = (Math.round(number * Math.pow(10, n)) * Math.pow(0.1, n)).toFixed(n);
     number = number.toString().split('.');
     number = [number[0], number[1] ? number[1].slice(0, n) : ""].join('.');
    }
    return number;
   };


//使用方法
import { retainDecimals } from "../utils/publicMethods"; //根据自己放的位置引入


在methods内写一个函数
methods: {
    //  保留数字小数点后两位数
    retainDecimalsFun(val, m) {
      return setDecimalBit(val, m);
    },
}


//在页面上引用
{{ retainDecimalsFun(想要改变的数字, 想要保留的位数:例如:2) }}
位置@/utils/publicMethods.js
 //函数防抖
export const debounce = (fn, delay) => {
	// 记录上一次的延时器
	var timer = null;
	var delay = delay || 200;
	return function() {
		var args = arguments;
		var that = this;
		// 清除上一次延时器
		clearTimeout(timer)
		timer = setTimeout(function() {
			fn.apply(that, args)
		}, delay);
	}
}
//函数节流
export const throttle = (func = () => {}, delay = 0) => {
	var timer = null;
	return function() {
		var context = this;
		var args = arguments;
		if (!timer) {
			timer = setTimeout(function() {
				func.apply(context, args);
				timer = null;
			}, delay);
		}
	}
}


//引入组件
import { debounce,throttle } from "../utils/publicMethods";


methods: {
    staffDebounceFun: debounce(function () {
      在这个位置调用要进行防抖的函数
    }, 300), // 间隔的毫秒数
}

//使用方法
<input type="text" @input="staffDebounceFun" />

不需要思考 拿过去就能用 !!!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue 3中结合TypeScript封装全局的防抖节流函数可以通过创建一个`utils`文件,并在Vue应用的入口文件中进行全局注册。下面是一个示例: ```typescript // utils/debounce.ts export function debounce(func: Function, delay: number) { let timer: NodeJS.Timeout | null = null; return function(...args: any[]) { if (timer) { clearTimeout(timer); } timer = setTimeout(() => { func.apply(this, args); }, delay); }; } // utils/throttle.ts export function throttle(func: Function, delay: number) { let timer: NodeJS.Timeout | null = null; let lastRun = 0; return function(...args: any[]) { const currentTime = Date.now(); if (timer) { clearTimeout(timer); } if (currentTime - lastRun >= delay) { func.apply(this, args); lastRun = currentTime; } else { timer = setTimeout(() => { func.apply(this, args); lastRun = Date.now(); }, delay); } }; } ``` 然后,在Vue应用的入口文件(如`main.ts`)中,可以将这些函数注册为全局方法: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import { debounce, throttle } from './utils'; const app = createApp(App); app.config.globalProperties.$debounce = debounce; app.config.globalProperties.$throttle = throttle; app.mount('#app'); ``` 现在,你可以在Vue组件中使用`this.$debounce`和`this.$throttle`来调用防抖节流函数了。 ```vue <template> <div> <input type="text" @input="handleInput" /> </div> </template> <script> export default { methods: { handleInput: this.$debounce(function(event) { console.log(event.target.value); }, 300), }, }; </script> ``` 在上面的示例中,`handleInput`方法使用了防抖函数,延迟300毫秒后执行。你可以根据实际需求调整防抖节流的延迟时间。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值