js 前端使用较多的通用方法

1.毫秒转时间

(如:3600000=>{days:0, hours:1, minutes:0, seconds:0});
例子:109380000=>{days: 1, hours: 6, minutes: 23, seconds: 0}。
const formatDuring = (time) => {
	const days = parseInt(time / (1000 * 60 * 60 * 24))
	const hours = parseInt((time % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
	const minutes = parseInt((time % (1000 * 60 * 60)) / (1000 * 60))
	const seconds = Math.floor((time % (1000 * 60)) / 1000)
	
	return {days, hours, minutes, seconds}
}

2.数组对象过滤重复的key数据

例子:const arr=[{key:1,value:11},{key:2,value:22},{key:1,value:33}]
			=>
			[{"key": 1,"value": 11},{"key": 2,"value": 22}]
export const nonredundantArr = (arr,key) => {
	const m = new Map();
	
	return arr.filter((ele) => !m.has(ele[key]) && m.set(ele[key], ""));
}

3.防抖

例子:const formatShops = (val=””) => {
	shopRun({pageNum: 1, pageSize: 300, shopNAme: val}).then((res) => {
	setShopList(res.data.list)
	}).catch((err) => {});
};
let debounceAjaxShop = debounce(formatShops, 500);
function debounce(fun, delay) {
	return function (args) {
		let that = this;
		let _args = args;
		clearTimeout(fun.id);
		fun.id = setTimeout(function () {
			fun.call(that, _args);
		}, delay);
	};
}

4.URL构建

const build = (url, params) => {
	// URL构建方法
	const ps = [];
	if (params) {
		for (let p in params) {
			if (p) {
				ps.push(p + "=" + encodeURIComponent(params[p]));
			}
		}
	}
	return url + "?" + ps.join("&");
}

5.表格单元格的Tooltip

const tipShowLine = (text,width=”110px”,line=2) => (
	<div
		style={{
		width,
		display: '-webkit-box',
		overflow: 'hidden',
		whiteSpace: 'normal !important',
		textOverflow: 'ellipsis',
		wordWrap: 'break-word',
		webkitLineClamp: line,
		webkitBoxOrient: 'vertical',
		}}
	>
		<Tooltip placement="topLeft" title={text}>
			{text ? <span>{text}</span> : '-'}
		</Tooltip>
	</div>
);

6.页面跳转带参history(目前在saas用)

// A code block
var foo = 'bar';
import { history } from 'umi';
export const navTo = (path: string, query: any) => {
	history.push({
		pathname: path,
		query,
	});
};

还会持续添加ing。。。
觉得还可以就点个赞吧👍

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值