(function(window,$){ function Tools() { } // url Tools.prototype.readUrlToParams = function() { var url = location.href; var nameValue; var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&"); var paraObj = {}; for (var i = 0; nameValue = paraString[i]; i++) { var name = nameValue.substring(0, nameValue.indexOf("=")); var value = nameValue.substring(nameValue.indexOf("=") + 1, nameValue.length); if (value.indexOf("#") > -1) { value = value.split("#")[0]; } paraObj[name] = value; } return paraObj; }//时间戳转化为多久以前 Tools.prototype.formatMsgTime = function(timespan) { // 转换成时间 var dateTime = new Date(timespan); // 获取时间戳对应的年月日是分秒 var year = dateTime.getFullYear(); var month = dateTime.getMonth() + 1; var day = dateTime.getDate(); var hour = dateTime.getHours(); var minute = dateTime.getMinutes(); var second = dateTime.getSeconds(); // 获取当前时间 var now = new Date(); // 转换成时间戳 var now_new = Date.parse(now); //typescript转换写法 // 初始变量 var milliseconds = 0; var timeSpanStr; // 时间戳作差求时间差 milliseconds = now_new - timespan; // 小于一分钟等于刚刚 if (milliseconds <= 1000 * 60 * 1) { timeSpanStr = '刚刚 :'; } // 小于一小时是多少分钟前 else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) { timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前 '; } // 小于一天是多少小时 else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) { timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前 '; } // 小于半个月是多少天前 else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) { timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前 '; } // 几个月 else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) { timeSpanStr = month + '-' + day + ' ' + hour + ':' + minute; } else { // 几年 timeSpanStr = year + '-' + month + '-' + day + ' ' + hour + ':' + minute; } return timeSpanStr; }; //时间转化(时间戳转化为标准时间) Tools.prototype.formatDateTime = function(inputTime) { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return y + '/' + m + '/' + d + ' ' + h + ':' + minute + ':' + second; }; // 时间戳的差转化时分秒 Tools.prototype.getNowFormatDate = function (timespan) { var date = timespan; //计算剩余的毫秒数 var h = parseInt(date / 1000 / 60 / 60 % 24 , 10); //计算剩余的小时 var M = parseInt(date / 1000 / 60 % 60, 10);//计算剩余的分钟 var s = parseInt(date / 1000 % 60, 10);//计算剩余的秒数 var seperator2 = ":"; if (h <= 9) { h = "0" + h; } if (M <= 9) { M = "0" + M; } if (s <= 9) { s = "0" + s; } var currentdate = h + seperator2 + M + seperator2 + s; return currentdate; }, // 获取分秒 Tools.prototype.getNowHS = function (timespan , newTime) { var date = timespan - newTime; //计算剩余的毫秒数 var M = parseInt(date / 1000 / 60 % 60, 10);//计算剩余的分钟 var s = parseInt(date / 1000 % 60, 10);//计算剩余的秒数 var seperator2 = ":"; if(M < 0 || s < 0){ return '00:00' } if (M <= 9 && M > 0) { M = "0" + M; } if (s <= 9 && s > 0) { s = "0" + s; } var currentdate = M + seperator2 + s; return currentdate; },// 获取n为随机数 Tools.prototype.randomNum = function(n) { var t = ''; for (var i = 0; i < n; i++) { t += Math.floor(Math.random() * 10); } return t; } // 图片预加载
Tools.prototype.preLoadImages = function(urls) {// 可以是String Array或者String
var argsLen = arguments.length,
loadImage = function (url) {
var img = new Image();
img.src = url;
};
if (argsLen == 1) {
if (typeof (urls) == "string" && (sLen = urls.length) > 1) {// 预加载一个图片
loadImage(urls);
}
else if (Object.prototype.toString.call(arguments[0]) == "[object Array]" && urls.length > 0) {// 预加载多个图片
for (var i = 0; i < urls.length; i++) {
loadImage(urls[i]);
}
}
}
}
window.Tools = Tools; })(window,$)