• 【JS】利用中国古老的天干地支给文件命名


    【需求】

    有若干实质为SQL的变量,取值后生成文件下载,需要拟定一个不容易重复的初始名,本以时间戳命名,后发现过长且不好记忆。

    【解决方案】

    从天干中取一个随机值,从地支中取一个随机值,合成初始名。此名兼具简短、重复率低和易记的特点。

    【JS代码】

    复制代码
        //----------------------------------------------
        // 使用天干地支生成名字,有简短重名率低的优势
        //----------------------------------------------
        function generateName(){
            var arr1=['甲','乙','丙','丁','戊','己','庚','辛','壬','癸'];
            var idx1=Math.floor(Math.random()*arr1.length);
    
            var arr2=['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'];
            var idx2=Math.floor(Math.random()*arr2.length);
    
            return arr1[idx1]+arr2[idx2];
        }
    复制代码

    【有图有真相】

     【全部代码】

    复制代码
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>天干地支命名法</title>
    
    <style type="text/css">
    
    </style>
    </head>
    <body>
        <button onclick="javascript:addName('nameOl');">Add</button>
        <ol id="nameOl">
        </ol>
    </body>
    </html>
    <script type="text/javascript">
    <!--
        function addName(olId){
            var ol=document.getElementById(olId);
    
            var li=document.createElement("li");
            li.appendChild(document.createTextNode(generateName()));
            ol.appendChild(li);
        }
    
        //----------------------------------------------
        // 使用天干地支生成名字,有简短重名率低的优势
        //----------------------------------------------
        function generateName(){
            var arr1=['甲','乙','丙','丁','戊','己','庚','辛','壬','癸'];
            var idx1=Math.floor(Math.random()*arr1.length);
    
            var arr2=['子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥'];
            var idx2=Math.floor(Math.random()*arr2.length);
    
            return arr1[idx1]+arr2[idx2];
        }
    //-->
    </script>
    复制代码

    END

  • 相关阅读:
    win10 UWP button
    内网分享资源
    内网分享资源
    CF724F Uniformly Branched Trees
    win10 UWP FlipView
    win10 UWP FlipView
    win10 UWP FlipView
    搭建阿里云 centos mysql tomcat jdk
    搭建阿里云 centos mysql tomcat jdk
    win10 UWP 申请微软开发者
  • 原文地址:https://www.cnblogs.com/heyang78/p/15733554.html
Copyright © 2020-2023  润新知