作为一名初级前端(bug)开发攻城狮,有什么是比白嫖更快乐的呢?除了饺子…不知道为什么最近总对饺子蛮感兴趣的…..
曹贼,我给你们准备了一些开发过程中比较常见的js函数块,碰见了直接嫖就行了。

函数的节流与防抖

节流
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
* 函数节流 规定时间内多次触发只执行一次
*/
function throttle(fn,delay){
var lastTime;
var timer;
var delay = delay || 200;
return function() {
var args = arguments;
// 记录当前函数触发的时间
var nowTime = Date.now();
if (lastTime && nowTime - lastTime < delay) {
clearTimeout(timer);
timer = setTimeout(function () {
// 记录上一次函数触发的时间
lastTime = nowTime;
// 修正this指向问题
fn.apply(this, args);
}, delay);
}else{
lastTime = nowTime;
fn.apply(this, args);
}
}
}

防抖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* 函数防抖 规定时间内结束后在触发,若在此时间内多次触发则重新计时
*/
function 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);
}
}

脱敏函数-用户隐私保护

姓名脱敏
1
2
3
4
5
6
7
8
9
10
11
12
13
function nameProtect(str) {
if (null != str && str != undefined) {
if (str.length <= 3) {
return "*" + str.substring(1, str.length);
} else if (str.length > 3 && str.length <= 6) {
return "**" + str.substring(2, str.length);
} else if (str.length > 6) {
return str.substring(0, 2) + "****" + str.substring(6, str.length);
}
} else {
return "";
}
}
身份证脱敏
1
2
3
4
5
6
function idCardProtect(value, row, index) {
var len = value.length;
var xx = value.substring(1, len - 1);
var values = value.replace(xx, "****************");
return values;
}
手机号脱敏
1
2
3
4
5
6
function mobileProtect(value, row, index) {
var len = value.length;
var xx = value.substring(3, len - 4);
var values = value.replace(xx, "****");
return values;
}

时间篇

根据秒数算展示时间
1
2
3
4
5
6
7
8
9
10
11
function setNumer(times) { 
var h = Math.floor(times/3600);
var m = Math.floor(times%3600/60);
var s = Math.floor(times%60);
//转换格式
h = h>9?h:"0"+h;
m = m>9?m:"0"+m;
s = s>9?s:"0"+s;
//在页面上显示
return {h:h,m:m,s:s};
}
根据两个时间差毫秒数计算相差几天
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
function diffTime(times) { //根据两个时间差毫秒数计算相差几天
var timediff = times;
// var weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
// timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

var days = Math.floor(timediff / (1000 * 60 * 60 * 24));
timediff -= days * (1000 * 60 * 60 * 24);

var hours = Math.floor(timediff / (1000 * 60 * 60));
timediff -= hours * (1000 * 60 * 60);

var mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);

var secs = Math.floor(timediff / 1000);
timediff -= secs * 1000;
// console.log(days + " 天, " + hours + " 小时, " + mins + " 分"+secs+"秒");
var time_diff = {
// weeks : weeks,
days: days,
hours: hours,
mins: mins,
secs: secs
}
return time_diff;
}
获得当前时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function getNowTime(type){
var date = new Date();
var year = date.getFullYear();
var month= date.getMonth()+1;
var day = date.getDate();
month = month>9?month:"0"+month;
day = day>9?day:"0"+day;
var str;
if(type==1){
str = year+"年"+month+"月"+day+"日"
}else{
str = year+"-"+month+"-"+day
}
return str
}
获得当前时间
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
function getNowTime(type){
var date = new Date();
var year = date.getFullYear();
var month= date.getMonth()+1;
var day = date.getDate();
month = month>9?month:"0"+month;
day = day>9?day:"0"+day;
var str;
if(type==1){
str = year+"年"+month+"月"+day+"日"
}else{
str = year+"-"+month+"-"+day
}
return str
}
判断当前(指定)时间是否在条件范围内
1
2
3
4
5
6
7
8
9
10
var curDate  = Date.parse('2020-12-18') //指定时间
# var curDate = Date.parse(new Date()) //当前时间
var beginDate = Date.parse(new Date('2020-12-12'))
var endDate = new Date('2020-12-25')
endDate = endDate.setDate(endDate.getDate()+1) //时间戳判断需要加一天(用于不知道截至时间)
if(curDate >= beginDate && curDate < endDate){
console.log('处于当前时间范围内')
}else{
console.log('不在当前时间范围内')
}

数组篇

普通数组去重
1
2
3
4
5
6
7
8
9
10
function arryClear(arr){
var tmp = new Array();
for(var i in arr){
if(tmp.indexOf(arr[i])==-1){
tmp.push(arr[i]);
}
}
return tmp;
}

数组对象去重复

参数说明:arry数组,Object根据的对象名

1
2
3
4
5
6
7
8
9
10
11
function arryClearObj(arry,Object){
var result = [];
var obj = {};
for(var j=0;j<arry.length;j++){
if(!obj[arry[j][Object]]){
result.push(arry[j]);
obj[arry[j][Object]] = true;
}
}
return result;
}

表单验证

验证手机号码
1
2
3
4
5
6
7
8
9
10
11
function checkPhone(num){
const zz = /^1[3456789]\d{9}$/;
if(!zz.test(num)){
uni.showToast({
title : "请输入正确的手机号码",
icon : "none"
});
return false
}
return true;
}
邮箱验证
1
2
3
4
5
6
7
8
9
10
11
function checkEmail(num){
const zz = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
if(!zz.test(num)){
uni.showToast({
title : "邮箱格式不正确",
icon : "none"
});
return false
}
return true;
}

内存字节转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function conver(limit){  
var size = "";
if( limit < 0.1 * 1024 ){ //如果小于0.1KB转化成B
size = limit.toFixed(2) + "B";
}else if(limit < 0.1 * 1024 * 1024 ){//如果小于0.1MB转化成KB
size = (limit / 1024).toFixed(2) + "KB";
}else if(limit < 0.1 * 1024 * 1024 * 1024 * 1024){ //如果小于0.1GB转化成MB
// size = (limit / (1024 * 1024)).toFixed(2) + "MB";
size = (limit / (1024 * 1024)).toFixed(2);
}else{ //其他转化成GB
size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB";
}
var sizestr = size + "";
var len = sizestr.indexOf("\.");
var dec = sizestr.substr(len + 1, 2);
if(dec == "00"){//当小数点后为00时 去掉小数部分
return sizestr.substring(0,len) + sizestr.substr(len + 3,2);
}
return sizestr;
}

随机生成字符串

好奇这种需求是怎么来的,讲道理哦,不应该是后端小伙伴做的吗,像这种涉及到随机字符串生成的东西…

1
2
3
4
5
6
7
8
9
var str = ['0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
function generateMixed(n) {
var res = "";
for(var i = 0; i < n ; i ++) {
var id = Math.ceil(Math.random()*35);
res += str[id];
}
return res;
}
× 我是好人
打赏二维码