JS(二十一)有用的JS函数(持续更新)

来源:互联网 发布:sql语句计算平均值 编辑:程序博客网 时间:2024/06/05 19:12

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 (func, wait) {        var context, args, timeout, throttling, more, result;        var whenDone = debounce(            function () {                more = throttling = false;            }, wait);        return function () {            context = this;            args = arguments;            var later = function () {                timeout = null;                if (more) {                    func.apply(context, args);                }                whenDone();            };            if (!timeout) {                timeout = setTimeout(later, wait);            }            if (throttling) {                more = true;            } else {                result = func.apply(context, args);            }            whenDone();            throttling = true;            return result;        };    };
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);        }    });}

原创粉丝点击