前端小知识

1.正则去除空格

function removeBlank(str) {
     return str.replace(/s+/g, '');
}        

2.倒计时写法

function showTime(options) {
        var defaults = {
            end: 0,
            diffTime: 0,
            displayTpl: "{hour}:{minute}:{second}",
            el: $('.countDown'),
            callback: function () {
            }
        };
        var options = $.extend({}, defaults, options);
        var sec, s, m, h;
        options.end = new Date(options.end.toString().replace(/-/g, '/')).getTime();

        function eachTime() {
            if (sec >= 0) {
                h = Math.floor(sec / (60 * 60));
                m = Math.floor(sec % (60 * 60) / 60);
                s = sec % (60 * 60) % 60;
                h = h < 10 ? ('0' + h) : h;
                m = m < 10 ? ('0' + m) : m;
                s = s < 10 ? ('0' + s) : s;
                options.el.html(options.displayTpl.replace("{hour}", h).replace("{minute}", m).replace("{second}", s));
                sec--;
            }
        }

        var time = setInterval(function () {
            options.nowTime = new Date().getTime() + options.diffTime;
            sec = Math.floor((options.end - options.nowTime) / 1000);

            if (sec >= 0) {
                eachTime();
            } else {
                clearInterval(time);
                options.callback && typeof (options.callback) === 'function' && options.callback();
            }
        }, 1000);
        return {
             showTime: showTime
         }
    };

3.控制台打印

function log(tips, data) {
        console.log('````````````' + tips + '````````````');
        console.log(data);
        console.log('```````````````````````````````````');
        console.log('           ')
}

4.设置浏览器的基数

var setWidth = function () {
        var w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        w = w > 640 ? 640 : w;
        w = w < 320 ? 320 : w;
        var htmlObj = $('html');
        var rem = w / 10 + 'px';
        htmlObj.css('font-size', rem);
}
原文地址:https://www.cnblogs.com/wangtao-/p/10496060.html