常用 js ts 函数

// js版explode
function explode(inputstring, separators, includeEmpties) {
    inputstring = new String(inputstring);
    separators = new String(separators);
    if (separators == "undefined") {
        separators = " :;";
    }
    fixedExplode = new Array(1);
    currentElement = "";
    count = 0;
    for (x = 0; x < inputstring.length; x++) {
        str = inputstring.charAt(x);
        if (separators.indexOf(str) != -1) {
            if (((includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) {} else {
                fixedExplode[count] = currentElement;
                count++;
                currentElement = "";
            }
        } else {
            currentElement += str;
        }
    }
    if ((!(includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
        fixedExplode[count] = currentElement;
    }
    return fixedExplode;
}

// 获取get参数
function getQueryStr(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}

// 获取指定范围随机数min ≤ r ≤ max
function randBetween(Min, Max) {
    var Range = Max - Min;
    var Rand = Math.random();
    var num = Min + Math.round(Rand * Range); //四舍五入
    return num;
}
/**
 * 获取get参数
 */
function getQueryStr(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
}
/**
 * 通过res name获取bitmap
 */
function createBitmapByName(name: string) {
    let result = new egret.Bitmap();
    let texture: egret.Texture = RES.getRes(name);
    if(!texture){
        console.log("no image:"+name );
    }
    result.texture = texture;
    return result;
}
/**
 * 截图 获取base64
 */
function doShot(obj:egret.DisplayObject,x:number=0,y=0,w=null,h=null){
    let renderTexture = new egret.RenderTexture;
    renderTexture.drawToTexture(obj,new egret.Rectangle(x,y,w,h));
    return renderTexture.toDataURL("image/jpeg",new egret.Rectangle(0,0,w,h));
}
/**
 * 发送ajax
 */
function ajax({url,data,success,type="POST"}){
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.withCredentials = false;
    xmlhttp.open(type, url, true);
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4){
            if(xmlhttp.status == 200){
                var res = xmlhttp.response
                success(JSON.parse(res));
            }else{
                success("error")
                console.log("ajax error");
            }
        }
    }
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;");
    xmlhttp.send(buildForm(data,null));
}
/**
 * 生成url
 */
function buildForm (data: any, query: any): string {
    let params: any = [];
    if (data) {
        for (let n in data) {
            params.push(encodeURIComponent(n) + "=" + encodeURIComponent(data[n]));
        }
    }
    if (query) {
        params = params.concat(query);
    }
    return params.join("&");
};
/**
 * 设置元素样式
 */
function setStyle(obj,style){
    for (var key in style) {
        let val = style[key];
        let sty = obj.style;
        sty[key] = val;
    }
}
/**
 * 判断是否PC
 */
function IsPC() {
    var userAgentInfo = navigator.userAgent;
    var Agents = ["Android", "iPhone",
                "SymbianOS", "Windows Phone",
                "iPad", "iPod"];
    var flag = true;
    for (var v = 0; v < Agents.length; v++) {
        if (userAgentInfo.indexOf(Agents[v]) > 0) {
            flag = false;
            break;
        }
    }
    return flag;
}
/**
 * 读取cookie
 **/
function getCookie(name)
{
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}
/**
 * 获取拓展名
 **/
function getExtension(filename)
{
    var index1 = filename.lastIndexOf("."); 
    if(index1 < 0){
        return null;
    }
    var index2 = filename.length;
    var postf  = filename.substring(index1+1,index2);
    return postf;
}

/**
 * 字符串填充
 **/
function strPad(str) {  
    var pad = "00000"  
    return pad.substring(0, pad.length - str.length) + str  
}  

// 获取web目录 无斜杠
function getRootPath() {
    var pathName = window.location.pathname.substring(1);
    var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
    if (webName == "") {
        return window.location.protocol + '//' + window.location.host;
    } else {
        return window.location.protocol + '//' + window.location.host + '/' + webName;
    }
}

// js数组打乱
function randomsort(a, b) {
    return Math.random()>.5 ? -1 : 1;
    //用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
}
var arr = [1, 2, 3, 4, 5];
arr.sort(randomsort);

相关文章

此处评论已关闭