| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- let library ={
- formatSeconds(value) {
- let result = parseInt(value);
- // let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
- // let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
- let m = Math.floor((result / 60 % 60)) < 10 ? '' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
- let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
- let res = '';
- // if(h !== '00') res += `${h}h`;
- // if(m !== '00') res += `${m}min`;
- // res += `${s}s`;
- // if(h !== '00') res += `${h}:`;
- res += `${m}:`;
- res += `${s}`;
- return res;
- },
- randomInt(min, max){
- return Math.floor(Math.random() * (max+1 - min)) + min;
- },
- getRandom(start, end, fixed=0) {
- //fixed 保留几位小数
- let differ = end - start
- let random = Math.random()
- return (start + differ * random).toFixed(fixed)
- },
- removeObj(arr,obj)
- {
- let index = this.indexOf(arr,obj);
- if (index > -1) {
- arr.splice(index, 1);
- }
- },
- indexOf(arr,obj) {
- for (var i = 0; i < arr.length; i++) {
- if (arr[i] == obj) return i;
- }
- return -1;
- },
- insert(arr,index, item) {
- arr.splice(index, 0, item);
- },
- //数组元素位置替换
- swapArray: function(arr, index1, index2) {
- arr[index1] = arr.splice(index2, 1, arr[index1])[0];
- return arr;
- },
- swapLeft : function(arr) {
- let element = arr.shift();
- arr.push(element);
- },
- //生成指定范围随机整数
- randomFromIntRange: function(min, max) {
- return Math.floor(Math.random() * (max - min)) + min;
- },
- happenRate: function(rate) {
- let result = this.randomFromIntRange(0,100);
- if(result<rate)
- {
- return true;
- }
- else
- {
- return false;
- }
- return;
- },
- openInWebview () {
- let ua = navigator.userAgent.toLowerCase()
- if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
- return false
- } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
- return false
- } else if (ua.match(/WeiBo/i) == "weibo") {
- return false
- } else {
- if (ua.match(/Android/i) != null) {
- return ua.match(/browser/i) == null
- } else if (ua.match(/iPhone/i) != null) {
- return ua.match(/safari/i) == null
- } else {
- return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
- }
- }
- },
- remove(arr, item) {
- let index = arr.indexOf(item);
- if (index > -1) {
- arr.splice(index, 1);
- }
- },
- setImageBase64(base64Url,callback){
- let img = new Image();
- img.src = base64Url;
- img.onload = function(){
- let texture = new cc.Texture2D();
- texture.initWithElement(img);
- texture.handleLoadedTexture();
- if (callback)
- callback(texture);
- }
- },
- };
- module.exports = library;
|