转前端开发中经常使用工具函数总结

一、时间格式化等方法app

 

推荐使用 moment.js 的库文件异步

 

三、模板、循环、MAP等方法使用函数

 

underscode.js 的方法性能

四、表单序列化成JSON this

 

复制代码
    $.fn.serializeJson = function() {
        var serializeObj = {};
        var array = this.serializeArray();
        var str = this.serialize();
        $(array).each(function() {
            if (serializeObj[this.name]) {
                if ($.isArray(serializeObj[this.name])) {
                    serializeObj[this.name].push(this.value);
                } else {
                    serializeObj[this.name] = [serializeObj[this.name], this.value];
                }
            } else {
                serializeObj[this.name] = this.value;
            }
        });
        return serializeObj;
    };
复制代码

 

五、字符串截取使用……填补spa

复制代码
    String.prototype.strcut = function(number) {
        var length = this.length;
        var tmp = this.substr(0, number);
        if (this.length > number) {
            tmp += "…";
        }
        return tmp;
    }
复制代码

六、时间格式为,xxxx 天,xxx分钟前,日期prototype

复制代码
    Date.prototype.Format = function(fmt, current) {

        if (current) {
            var diff = current - this.getTime();
            if (diff < 5 * 60 * 1000) {
                return "刚刚";
            }
            if (diff < 60 * 60 * 1000) {
                return (Math.floor(diff / (60 * 1000))) + "分钟前";
            }
            if (diff < 24 * 60 * 60 * 1000) {
                return (Math.floor(diff / (60 * 60 * 1000))) + "小时前";
            }

            if (diff < 30 * 24 * 60 * 60 * 1000) {
                return (Math.floor(diff / (24 * 60 * 60 * 1000))) + "天前";
            }

            if (diff < 12 * 30 * 24 * 60 * 60 * 1000) {
                return (Math.floor(diff / (30 * 24 * 60 * 60 * 1000))) + "月前";
            }

            if (diff > 12 * 30 * 24 * 60 * 60 * 1000) {
                return (Math.floor(diff / (12 * 30 * 24 * 60 * 60 * 1000))) + "年前";
            }
        }

        var o = {
            "Y+": this.getFullYear(), //月份 
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };
复制代码

七、解析URLcode

复制代码
function parseUrl() {
            var arr = location.search.split('?')[1].split('&');
            var params = {};
            for (var i = 0, l = arr.length; i < l; i++) {
                var param = arr[i].split('=');
                params[param[0]] = param[1];
            }
            return params;
        }
复制代码

八、获取get参数orm

复制代码
function getParameterByName(name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
        }
复制代码

 

九、函数节流,让频繁事件触发更稀疏提升性能,例如及时搜索功能。使用方法为fn 为事件响应函数,delay 为间隔时间,调用 throttle(fn,delay) 返回一个新的函数给事件便可blog

复制代码
        function throttle(fn, delay) {
            var timer = null;
            return function() {
                var context = this,
                    args = arguments;
                clearTimeout(timer);
                timer = setTimeout(function() {
                    fn.apply(context, args);
                }, delay);
            };
        }
复制代码

 

十、防止表单屡次提交,和9中同样,返回一个新的函数

复制代码
        /**
         * 防止屡次点击函数
         *
         * fn 完成时调用回调
         *  function fn(event,end) {
         *       (typeof end === "function") && end(); // 操做完成
         *  }
         */function noRepeateTap(fn) {
            var $obj;
            return function(event) {
                $obj = $(this);
                if ($obj.data("loadding") === "true") {
                    return;
                }
                $obj.data("loadding", "true").addClass('loadding');
                fn.call(this, event, function end() {
                    $obj.data("loadding", "").removeClass('loadding');
                    return;
                });
            }
        }
复制代码

 

第10个的使用例子

 

复制代码
// 绑定事件
$(container).on('click', '.btn-cancel', noRepeateTap(cancel));


    // 事件响应函数
    function cancel(event, end) {
        event.preventDefault();
             // 模拟异步请求
              setTimeout(function(){
            end(); // 须要手动调用注入的完成函数,通知完成,函数自动添加loadding class 类,用于调整样式,完成后自动移除
        },5000)
    }
复制代码
相关文章
相关标签/搜索