• js添加的一些工具方法


     1. 判断当前所在的环境:

    /*
     * 判断运行环境
     */
    const _uas = navigator.userAgent.toLowerCase();
    const isWho = {
        is_weixin: function() {
            if (_uas.match(/MicroMessenger/i) == "micromessenger") {
                return true;
            } else {
                return false;
            }
        },
        is_ios: function() {
            if (_uas.match(/iphone/i) || _uas.match(/ipad/i) || _uas.match(/ipod/i)) {
                return true;
            } else {
                return false;
            }
        },
        is_android: function() {
            if (_uas.match(/android/i)) {
                return true;
            } else {
                return false;
            }
        },
        is_weibo: function() {
            if (_uas.match(/weibo/i)) {
                return true;
            } else {
                return false;
            }
        }
    }
    View Code

      获取特定url参数

    /*
     * URL获取参数
     * 
     * @param {String} name 要获取的参数名称
     * @return {String} 获取的参数值
     */
    const getUrl = function(name) {
        // console.log(decodeURI((RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ''])[1]));
        return decodeURI(
            (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search) || [, ''])[1]
        );
    }
    View Code

      数组对象深拷贝

        this.alldeepCopy = function(o) {
            if (o instanceof Array) {
                var n = [];
                for (var i = 0; i < o.length; ++i) {
                    n[i] = this.alldeepCopy(o[i]);
                }
                return n;
    
            } else if (o instanceof Object) {
                var n = {}
                for (var i in o) {
                    n[i] = this.alldeepCopy(o[i]);
                }
                return n;
            } else {
                return o;
            }
        }

     2.  js处理浮点数的加减乘除工具方法

      js toFixed四舍五入精度bug处理

     3. js拷贝到粘贴板

        input的拷贝

    function copyText() {
     var elem = document.getElementById(id);
     
      elem.select();
      document.execCommand("Copy");
    
      alert("Copied the text);
    }

      拷贝其他标签的文本,如span

    function CopyToClipboard(containerid) {
        var range = document.createRange();
         range.selectNode(document.getElementById(containerid));
         window.getSelection().addRange(range);
         document.execCommand("copy");
         alert("text copied") ;
    }

     4. 对各种时间格式转换

    //时间戳转时间
    function stampToTime(dateNum: any, type, defaultVal = "- -") {
        if (!dateNum) {
            if (dateNum !== 0) {
                return defaultVal
            }
        }
        if (typeof (dateNum) == "string") {
            if(dateNum.indexOf('-') == -1){
                if (dateNum.length == 13) {
                    dateNum = parseInt(dateNum);
                } else {
                    return dateNum
                }
            }
        }
        let date = new Date(dateNum);
        let month = date.getMonth() + 1;
        if(type == 1){ //2018-5-5 12:07:05
            return date.getFullYear() + "-" + month + "-" + date.getDate() + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
        };
        if(type == 2){ //2018-05-05 12:07:05
            return date.getFullYear() + "-" + fixZero(month, 2) + "-" + fixZero(date.getDate(), 2) + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
        };
        if(type == 3){ //2018/5/5 12:07:05
            return date.getFullYear() + "/" + month + "/" + date.getDate() + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
        };
        if(type == 4){ //2018/05/05 12:07:05
            return date.getFullYear() + "/" + fixZero(month, 2) + "/" + fixZero(date.getDate(), 2) + " " + fixZero(date.getHours(), 2) + ":" + fixZero(date.getMinutes(), 2) + ":" + fixZero(date.getSeconds(), 2)
        };
        if(type == 5){ //2018-5-5
            return date.getFullYear() + "-" + month + "-" + date.getDate()
        };
        if(type == 6){ //2018-05-05
            return date.getFullYear() + "-" + fixZero(month, 2) + "-" + fixZero(date.getDate(), 2)
        };
        if(type == 7){ //2018/5/5
            return date.getFullYear() + "/" + month + "/" + date.getDate()
        };
        if(type == 8){ //2018/05/05
            return date.getFullYear() + "/" + fixZero(month, 2) + "/" + fixZero(date.getDate(), 2)
        };
        if(type == 9){ //2018年05月05
            return date.getFullYear() + "年" + fixZero(month, 2) + "月" + fixZero(date.getDate(), 2) + "日"
        };
        if(type == 10){ //20180505120705
            return date.getFullYear() + fixZero(month, 2) + fixZero(date.getDate(), 2) + fixZero(date.getHours(), 2) + fixZero(date.getMinutes(), 2) + fixZero(date.getSeconds(), 2)
        };
    }

     5. js hex String和 byteArray转换

    // Convert a hex string to a byte array
    function hexToBytes(hex) {
        for (var bytes = [], c = 0; c < hex.length; c += 2)
        bytes.push(parseInt(hex.substr(c, 2), 16));
        return bytes;
    }
    
    // Convert a byte array to a hex string
    function bytesToHex(bytes) {
        for (var hex = [], i = 0; i < bytes.length; i++) {
            var current = bytes[i] < 0 ? bytes[i] + 256 : bytes[i];
            hex.push((current >>> 4).toString(16));
            hex.push((current & 0xF).toString(16));
        }
        return hex.join("");
    }

     6.防抖节流函数

     1 function debounce(func, delay) {
     2     let timeout
     3     return function () {
     4         clearTimeout(timeout) // 如果持续触发,那么就清除定时器,定时器的回调就不会执行。
     5         timeout = setTimeout(() => {
     6             func.apply(this, arguments)
     7         }, delay)
     8     }
     9 }
    10 
    11 
    12 function throttle(func, deley) {
    13     let run = true
    14     return function () {
    15         if (!run) {
    16             return // 如果开关关闭了,那就直接不执行下边的代码
    17         }
    18         run = false // 持续触发的话,run一直是false,就会停在上边的判断那里 
    19         setTimeout(() => {
    20             func.apply(this, arguments)
    21             run = true // 定时器到时间之后,会把开关打开,我们的函数就会被执行 
    22         }, deley)
    23     }
    24 }

     7. str和arrybuffer转换

     function ab2str(buf) {
            return String.fromCharCode.apply(null, new Uint16Array(buf));
        }
    
    function str2ab(str) {
            let buf = new ArrayBuffer(str.length * 2);
            let bufView = new Uint16Array(buf);
            for (let i = 0, strLen = str.length; i < strLen; i++) {
                bufView[i] = str.charCodeAt(i);
            }
            return buf;
        }
  • 相关阅读:
    nginx学习编译安装(1)
    媒体查询
    web前端开发--超链接
    web前端开发--列表
    web前端开发--格式化文本与段落
    DIV与SPAN
    CSS基础
    表的创建
    数据库存储结构
    关系完整性约束
  • 原文地址:https://www.cnblogs.com/johnzhu/p/5970481.html
Copyright © 2020-2023  润新知