JS(二十一)JS函数库封装(持续更新)

1、数字转化为千分位分割符格式

 

window.convertThousands = function (num) {
        var prefix, suffix, sign;
        if (isNaN(Number(num))) {
            return num;
        }

        sign = (num >= 0) ? '' : '-';
        num = (Math.abs(num) || 0).toString();
        if (num.indexOf('.') >= 0) {
            prefix = num.split('.')[0];
            suffix = num.split('.')[1];
        }
        else {
            prefix = num;
        }

        var result = '';
        while (prefix.length > 3) {
            result = ',' + prefix.slice(-3) + result;
            prefix = prefix.slice(0, prefix.length - 3);
        }
        if (prefix) {
            result = sign + prefix + result;
        }

        if (suffix) {
            result = result + '.' + suffix;
        }
        return result;
    };

 

2、函数节流和去抖

 

 

window.debounce = function (func, wait, immediate) {
        var timeout;
        return function () {
            var context = this, args = arguments;
            var later = function () {
                timeout = null;
                if (!immediate) {
                    func.apply(context, args);
                }
            };
            if (immediate && !timeout) {
                func.apply(context, args);
            }
            clearTimeout(timeout);
            timeout = setTimeout(later, wait);
        };
    };

    /* dependent on debounce */
    window.throttle = function (fn, interval) {
        var _self = fn,
            timer,
            firstTime = true;
        return function(){
            var args = arguments,
                _me = this;
            if(firstTime){
                _self.apply(_me, args);
                return firstTime = false;
            }
            if(timer){
                return flase;
            }
            timer = setTimeout(function(){
                clearTimeout(timer);
                timer = null;
                _self.apply(_me, args);
            }, interval || 500);
        };
    };
    //调用
    window.onresize = throttle(function(){
        console.log(1);
    }, 500);

3、计算两个日期相差的日期年月日等

/* 计算两日期相差的日期年月日等 */
    Date.prototype.dateDiff = function (objDate2, interval) {
        var d = this, i = {}, t = d.getTime(), t2 = objDate2.getTime();
        i['y'] = objDate2.getFullYear() - d.getFullYear();
        i['q'] = i['y'] * 4 + Math.floor(objDate2.getMonth() / 4) - Math.floor(d.getMonth() / 4);
        i['m'] = i['y'] * 12 + objDate2.getMonth() - d.getMonth();
        i['ms'] = objDate2.getTime() - d.getTime();
        i['w'] = Math.floor((t2 + 345600000) / (604800000)) - Math.floor((t + 345600000) / (604800000));
        i['d'] = Math.floor(t2 / 86400000) - Math.floor(t / 86400000);
        i['h'] = Math.floor(t2 / 3600000) - Math.floor(t / 3600000);
        i['n'] = Math.floor(t2 / 60000) - Math.floor(t / 60000);
        i['s'] = Math.floor(t2 / 1000) - Math.floor(t / 1000);
        return i[interval];
    };

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------

2017.12.15 更新

4、获取url中的参数

 

/* 获取URL查询参数 */
    window.getQueryString = function (param) {
        var sQuery = window.location.search;
        if (sQuery && sQuery.length > 0) {
            var arr = sQuery.substr(1).split('&');
            for (var i in arr) {
                var pair = arr[i].split('=');
                if (param && param.toLowerCase() === pair[0].toLowerCase() && pair[1]) {
                    return pair[1];
                }
            }
        }
        return '';
    };

5、封装jquery的ajax方法

 

var getDataSource = function (api, dataparam, successFunc, errorFunc) {
    $.ajax({
        url: api,
        type: 'GET',
        data: dataParam
        dataType: 'json',
        contentType: 'application/json',
        success: function (response) {
            successFunc.call(this, response);
        },
        error: function () {
            errorFunc.call(this);
        }
    });
}

6、js断言函数

 

var assert = function(value, desc){
    var li = document.createElement('li');
    li.className = value ? 'pass' : 'fail';
    li.appendChild(document.createTextNode(desc));
    document.getElementById('result').appendChild(li);
}

分配一个pass或者fail的样式,该样式却绝育断言参数的值value,并将新元素附加到body内的列表元素上。

----------------------------------------------------------------

2018.3.27更新

8、js替换换行符和回车符

//替换所有的回车换行    
var TransferString = function(content) {    
    var string = content;    
    try{    
        string=string.replace(/\r\n/g,"<br>")    
        string=string.replace(/\n/g,"<br>");    
    }catch(e) {    
        alert(e.message);    
    }    
    return string;    
} 

2018.10.16更新

9、去除对象多余属性(使用loadsh)

let model = {
    edb: null,
    cloud: null
};
param = _.pick(dataParam, _.keys(model));

model中是用户想要保存下来的属性,dataParam是要处理的对象。

10、判断变量类型

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fullstack_lth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值