博客
关于我
JS - 常用函数
阅读量:485 次
发布时间:2019-03-06

本文共 1508 字,大约阅读时间需要 5 分钟。

1. 生成随机颜色
function getRandomColor() {
return '#' + Math.floor(Math.random() * 16777215).toString(16);
}
2.生成相近颜色
function sameColor(color: string, n: number) {
const hexDigits = color.length === 6 ? color.match(/(\d|[a-f])/g) : color.match(/(\d|[a-f])/g);
let newColor = '';
for (let i = 0; i < hexDigits.length; i++) {
const value = hexDigits[i];
const improvedValue = parseInt(value, 16);
const randomOffset = Math.random() * n;
const half = 0.5;
const variation = randomOffset > half ? -1 : 1;
const finalValue = Math.floor(improvedValue + variation * Math.random() * n);
newColor += finalValue.toString(16);
}
return newColor;
}
3. 获取url参数
function getParam(name: string) {
const reg = new RegExp('(^|&StartTime=' + name + '=(\\S+)&?', 'i');
const result = window.location.search.match(reg);
return result ? decodeURIComponent(result[2]) : null;
}
4. 将数字转成每隔3位加一个逗号
function addCommas(value: string) {
const num = value.replace('.', '');
const integPart = num.replace(/[^0-9]/g, '');
let result = '';
while (integPart.length > 3) {
result = ',' + integPart.slice(-3) + result;
integPart = integPart.slice(0, -3);
}
return integPart + (num.match(/\./) ? '.' + num.split('.').slice(1) : '');
}

转载地址:http://yhodz.baihongyu.com/

你可能感兴趣的文章
nodejs生成多层目录和生成文件的通用方法
查看>>
nodejs端口被占用原因及解决方案
查看>>
Nodejs简介以及Windows上安装Nodejs
查看>>
nodejs系列之express
查看>>
nodejs系列之Koa2
查看>>
Nodejs连接mysql
查看>>
nodejs连接mysql
查看>>
NodeJs连接Oracle数据库
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
NodeMCU教程 http请求获取Json中文乱码解决方案
查看>>
Nodemon 深入解析与使用
查看>>
NodeSession:高效且灵活的Node.js会话管理工具
查看>>
node~ http缓存
查看>>
node不是内部命令时配置node环境变量
查看>>
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>
node中的get请求和post请求的不同操作【node学习第五篇】
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>