内含95个工具函数 前端大佬都在使用的JavaScript工具函数宝典( 六 )

// 比如 sleep(1000) 意味着等待1000毫秒,还可从 Promise、Generator、Async/Await 等角度实现 。// Promiseconst sleep = time => {return new Promise(resolve => setTimeout(resolve, time));};sleep(1000).then(() => {console.log(1);});// Generatorfunction* sleepGenerator(time) {yield new Promise(function(resolve, reject) {setTimeout(resolve, time);});}sleepGenerator(1000).next().value.then(() => {console.log(1);});//asyncfunction sleep(time) {return new Promise(resolve => setTimeout(resolve, time));}async function output() {let out = await sleep(1000);console.log(1);return out;}output();function sleep(callback, time) {if (typeof callback === "function") {setTimeout(callback, time);}}function output() {console.log(1);}sleep(output, 1000);61 清除脚本内容function stripscript(s) {return s.replace(/<script.*?>.*?</script>/gi, "");}62 判断是否以某个字符串开头String.prototype.startWith = function(s) {return this.indexOf(s) == 0;};63 时间个性化输出功能/*1、< 60s, 显示为“刚刚”2、>= 1min && < 60 min, 显示与当前时间差“XX分钟前”3、>= 60min && < 1day, 显示与当前时间差“今天 XX:XX”4、>= 1day && < 1year, 显示日期“XX月XX日 XX:XX”5、>= 1year, 显示具体日期“XXXX年XX月XX日 XX:XX”*/function timeFormat(time) {var date = new Date(time),curDate = new Date(),year = date.getFullYear(),month = date.getMonth() + 10,day = date.getDate(),hour = date.getHours(),minute = date.getMinutes(),curYear = curDate.getFullYear(),curHour = curDate.getHours(),timeStr;if (year < curYear) {timeStr = year + "年" + month + "月" + day + "日 " + hour + ":" + minute;} else {var pastTime = curDate - date,pastH = pastTime / 3600000;if (pastH > curHour) {timeStr = month + "月" + day + "日 " + hour + ":" + minute;} else if (pastH >= 1) {timeStr = "今天 " + hour + ":" + minute + "分";} else {var pastM = curDate.getMinutes() - minute;if (pastM > 1) {timeStr = pastM + "分钟前";} else {timeStr = "刚刚";}}}return timeStr;}64 半角转换为全角函数function toDBC(str) {var result = "";for (var i = 0; i < str.length; i++) {code = str.charCodeAt(i);if (code >= 33 && code <= 126) {result += String.fromCharCode(str.charCodeAt(i) + 65248);} else if (code == 32) {result += String.fromCharCode(str.charCodeAt(i) + 12288 - 32);} else {result += str.charAt(i);}}return result;}65 全角转换为半角函数function toCDB(str) {var result = "";for (var i = 0; i < str.length; i++) {code = str.charCodeAt(i);if (code >= 65281 && code <= 65374) {result += String.fromCharCode(str.charCodeAt(i) - 65248);} else if (code == 12288) {result += String.fromCharCode(str.charCodeAt(i) - 12288 + 32);} else {result += str.charAt(i);}}return result;}66 金额大写转换函数function transform(tranvalue) {try {var i = 1;var dw2 = new Array("", "万", "亿"); //大单位var dw1 = new Array("拾", "佰", "仟"); //小单位var dw = new Array("零","壹","贰","叁","肆","伍","陆","柒","捌","玖");//整数部分用//以下是小写转换成大写显示在合计大写的文本框中//分离整数与小数var source = splits(tranvalue);var num = source[0];var dig = source[1];//转换整数部分var k1 = 0; //计小单位var k2 = 0; //计大单位var sum = 0;var str = "";var len = source[0].length; //整数的长度for (i = 1; i <= len; i++) {var n = source[0].charAt(len - i); //取得某个位数上的数字var bn = 0;if (len - i - 1 >= 0) {bn = source[0].charAt(len - i - 1); //取得某个位数前一位上的数字}sum = sum + Number(n);if (sum != 0) {str = dw[Number(n)].concat(str); //取得该数字对应的大写数字,并插入到str字符串的前面if (n == "0") sum = 0;}if (len - i - 1 >= 0) {//在数字范围内if (k1 != 3) {//加小单位if (bn != 0) {str = dw1[k1].concat(str);}k1++;} else {//不加小单位,加大单位k1 = 0;var temp = str.charAt(0);if (temp == "万" || temp == "亿")//若大单位前没有数字则舍去大单位str = str.substr(1, str.length - 1);str = dw2[k2].concat(str);sum = 0;}}if (k1 == 3) {//小单位到千则大单位进一k2++;}}//转换小数部分var strdig = "";if (dig != "") {var n = dig.charAt(0);if (n != 0) {strdig += dw[Number(n)] + "角"; //加数字}var n = dig.charAt(1);if (n != 0) {strdig += dw[Number(n)] + "分"; //加数字}}str += "元" + strdig;} catch (e) {return "0元";}return str;}//拆分整数与小数function splits(tranvalue) {var value = https://www.isolves.com/it/cxkf/yy/js/2020-08-05/new Array("", "");temp = tranvalue.split(".");for (var i = 0; i


推荐阅读