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

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

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、NPM安装配置步骤(windows版本)
查看>>
nodejs与javascript中的aes加密
查看>>
nodejs中Express 路由统一设置缓存的小技巧
查看>>
nodejs中express的使用
查看>>
Nodejs中的fs模块的使用
查看>>
NodeJS使用淘宝npm镜像站的各种姿势
查看>>
nodejs包管理工具对比:npm、Yarn、cnpm、npx
查看>>
NodeJs单元测试之 API性能测试
查看>>
nodejs图片转换字节保存
查看>>
nodejs在Liunx上的部署生产方式-PM2
查看>>
nodejs字符与字节之间的转换
查看>>
NodeJs学习笔记001--npm换源
查看>>
NodeJs学习笔记002--npm常用命令详解
查看>>
nodejs学习笔记一——nodejs安装
查看>>
NodeJS实现跨域的方法( 4种 )
查看>>
nodejs封装http请求
查看>>
nodejs常用组件
查看>>
nodejs开发公众号报错 40164,白名单配置找不到,竟然是这个原因
查看>>
Nodejs异步回调的处理方法总结
查看>>
NodeJS报错 Fatal error: ENOSPC: System limit for number of file watchers reached, watch ‘...path...‘
查看>>