• 常用函数工具记录贴


    由于老大教我要把经常用过的函数记起来,所以从现在开始要吧用过的,看过的,别人好用的函数都集合在这里,如有侵犯到的请留言通知。

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

        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;
        };
     

    2、解析URL

    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;
            }
     

    3、获取get参数

    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, " "));
            }

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

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

    5、防止表单多次提交,返回一个新的函数

            /**
             * 防止多次点击函数
             *
             * 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;
                    });
                }
            }

     6、绑定事件

    // 绑定事件
    $(container).on('click', '.btn-cancel', noRepeateTap(cancel));
    
    
        // 事件响应函数
        function cancel(event, end) {
            event.preventDefault();
                 // 模拟异步请求
                  setTimeout(function(){
                end(); // 需要手动调用注入的完成函数,通知完成,函数自动添加loadding class 类,用于调整样式,完成后自动移除
            },5000)
        }
  • 相关阅读:
    py4 程序流程控制
    [Selenium+Java] Handling AJAX Call in Selenium Webdriver
    [Selenium+Java] SSL Certificate Error Handling in Selenium
    [Selenium+Java] Desired Capabilities in Selenium
    [Selenium+Java] How to use AutoIT with Selenium
    [Selenium+Java] https://www.guru99.com/using-robot-api-selenium.html
    [Selenium+Java] Selenium with HTMLUnit Driver & PhantomJS
    [Selenium+Java] Log4j with Selenium Tutorial
    [Selenium+Java] Parallel Execution & Session Handling in Selenium
    [Selenium+Java] How to Take Screenshot in Selenium WebDriver
  • 原文地址:https://www.cnblogs.com/mrdoor/p/4901784.html
Copyright © 2020-2023  润新知