zoukankan      html  css  js  c++  java
  • 测试定时器、获取字符串的字节长度

    测试定时器

    <html>
        <head>
            <meta charset="UTF-8">
            <title></title>
            <style>
                #txt{
                    300px;
                    height:300px;
                    display: block;
                    margin:100px  auto;
                }
            </style>
            <script>
                window.onload=function(){
                    var oTxt=document.getElementById("txt");
                    var timer=null;
                    var oldT=new Date().getTime();       //老时间
                    timer=setInterval(function(){
                        var now=new Date().getTime();  //新时间
                        oTxt.value+=now-oldT+'
    ';      //存放时间差
                        oldT=new Date().getTime();  
                    },1000);
                    document.onclick=function(){
                        clearInterval(timer);
                    }
                }
            </script>
        </head>
        <body>
            <textarea id="txt" rows="30" cols="30"></textarea>
        </body>
    </html>

    获取字符串的字节长度:

    <html>
        <head>
            <meta charset="utf-8">
            <title></title>
            <script>
                var str='abc啊';
                function getByLon(str,type){
                    var res=0;
                    for(var i=0;i<str.length;i++){
                        if(str.charAt(i)>='u4e00'&&str.charAt(i)<='u9fa5'){  //在4e00到9fa5之间的字符
                            if(type=='utf-8'){  //utf-8,字符加3
                                res+=3;
                            }else{res+=2;}    //其他字符加2
                        }else{res++}         //不在4e00到9fa5之间的字符
                    }
                    return res;
                }
                alert(getByLon(str,'utf-8'));
            </script>
        </head>
        <body>
        </body>
    </html>
  • 相关阅读:
    范德蒙矩阵相关
    bat运行exe程序
    github 用token远程连接(三)
    为什么将样本方差除以N1?
    Git commit格式 详解(二)
    C++中this与*this的区别
    函数末尾加入const的作用
    git 使用小补充(四)
    人工智能 机器学习
    机器学习分类
  • 原文地址:https://www.cnblogs.com/yang0902/p/5705355.html
Copyright © 2011-2022 走看看