zoukankan      html  css  js  c++  java
  • 自己写的一些公共js方法

    /* 
    
    说明文件:这里用的都是es6的语法 导入导出,拿vue举个栗子,你只需要在用到的地方,按需要导入就行了,然后在mounted中直接可以拿来用
    比如下面的手机****方法,在需要用到的地方import  
    不揍1:import { hidePhone } from '../utils/utils.js'
    不揍2:  
      mounted(){
           let hh=hidePhone(15308331208)
           console.log(hh,'222222222222');
      }
    
    */
    
    
    
    /* @1 隐藏中间四位数的电话好嘛**** */
    function hidePhone(phone){
        // var tel = phone;
        // // console.log(typeof tel);
        // tel = "" + tel;
        // // console.log(typeof tel);
        // var reg=/(d{3})d{4}(d{4})/;
        // var tel1 = tel.replace(reg, "$1****$2")
        // console.log(tel1);
        /* 第二种方法,随意选一种嘛  都是先转换成字符串的 */
        var tel = phone;
        tel = "" + tel;
        var ary = tel.split("");
        console.log(ary);
        ary.splice(3,4,"****");
        var tel1=ary.join("");
        return tel1 //153****1208
    }
    /*@2 年月日这个就有点多了(根据需求定制) */
    
    
    
    /* 当前时间转换为年月日时分秒 */
    function formatDate() {
        var date = new Date();
        var year = date.getFullYear();
        var month = addZero(date.getMonth() + 1);
        var day = addZero(date.getDate());
        var hours = addZero(date.getHours());
        var minutes = addZero(date.getMinutes());
        var seconds = addZero(date.getSeconds());
        // return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
        return year + '-' + month + '-' + day;
    }
    /* 配合上面的函数一起使用 */
    function addZero(num) {
        return num < 10 ? '0' + num : num;
    }
    
    /* @3 倒计时 这里只是说一下倒计时怎么计算,vue中直接拿过去用let hour 变成this.hour类推就行了*/
    function countDown(residue){
        setInterval(()=> {
            let day = parseInt(residue / (24 * 3600)); //剩余天数
            let hour = parseInt((residue) % (24 * 3600) / 3600); //剩余小时
            let minute = parseInt((residue) % 3600 / 60); //剩余分钟
            let second = parseInt((residue) % 60);  //剩余秒数
            residue--
            console.log(day+'天'+hour+'小时'+minute+'分钟'+second+'秒')// 打印'1天1小时0分钟55秒'
          },1000)
    }
    
    /* @4 加减乘除 */
    // 加法
    function accAdd(num1,num2){
        var r1,r2,m;
        try{
          r1 = num1.toString().split('.')[1].length;
        }catch(e){
          r1 = 0;
        }
        try{
          r2=num2.toString().split(".")[1].length;
        }catch(e){
          r2=0;
        }
        m=Math.pow(10,Math.max(r1,r2)); //pow() 方法可返回 x 的 y 次幂的值。
        // return (num1*m+num2*m)/m;
        return Math.round(num1*m+num2*m)/m;//round() 方法可把一个数字舍入为最接近的整数。然后除以10的N次方
    }
    // 减法
    function accSub(arg1,arg2){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        //last modify by deeka
        //动态控制精度长度
        n=(r1>=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(n);
    }
    // 乘法
    function accMul(arg1, arg2) {
        var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
        try { m += s1.split(".")[1].length } catch (e) {}
        try {m += s2.split(".")[1].length} catch (e) {}
    
        return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)  //pow() 方法可返回 x 的 y 次幂的值
    }
    //除法(要配合乘法函数一起使用)
    function accDiv(arg1,arg2){
        var t1=0,t2=0,r1,r2;
        try{t1=arg1.toString().split(".")[1].length}catch(e){}
        try{t2=arg2.toString().split(".")[1].length}catch(e){}
    
        r1=Number(arg1.toString().replace(".",""))
        r2=Number(arg2.toString().replace(".",""))
    //        return (r1/r2)*pow(10,t2-t1); //改变这一句,这里相当于又是乘法了 so改变一下
        return accMul(r1/r2,Math.pow(10,t2-t1))//调取乘法函数
    }
    
    
    
    
    
    
    
    
    
    
    
    
    export {hidePhone,formatDate,countDown,accAdd,accSub,accMul,accDiv} 
    

      

  • 相关阅读:
    scrapy框架
    selenium解析
    xpath解析
    解析语法
    request-html-render
    牛逼的requests-html
    Beautifulsoup
    请求和响应
    reuqests请求
    Django文件上传下载与富文本编辑框
  • 原文地址:https://www.cnblogs.com/myfirstboke/p/10640966.html
Copyright © 2011-2022 走看看