1、数字转化为千分位分割符格式
window.convertThousands = function (num) {
var prefix, suffix, sign;
if (isNaN(Number(num))) {
return num;
}
sign = (num >= 0) ? '' : '-';
num = (Math.abs(num) || 0).toString();
if (num.indexOf('.') >= 0) {
prefix = num.split('.')[0];
suffix = num.split('.')[1];
}
else {
prefix = num;
}
var result = '';
while (prefix.length > 3) {
result = ',' + prefix.slice(-3) + result;
prefix = prefix.slice(0, prefix.length - 3);
}
if (prefix) {
result = sign + prefix + result;
}
if (suffix) {
result = result + '.' + suffix;
}
return result;
};
2、函数节流和去抖
window.debounce = function (func, wait, immediate) {
var timeout;
return function () {
var context = this, args = arguments;
var later = function () {
timeout = null;
if (!immediate) {
func.apply(context, args);
}
};
if (immediate && !timeout) {
func.apply(context, args);
}
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
};
/* dependent on debounce */
window.throttle = function (fn, interval) {
var _self = fn,
timer,
firstTime = true;
return function(){
var args = arguments,
_me = this;
if(firstTime){
_self.apply(_me, args);
return firstTime = false;
}
if(timer){
return flase;
}
timer = setTimeout(function(){
clearTimeout(timer);
timer = null;
_self.apply(_me, args);
}, interval || 500);
};
};
//调用
window.onresize = throttle(function(){
console.log(1);
}, 500);
3、计算两个日期相差的日期年月日等
/* 计算两日期相差的日期年月日等 */
Date.prototype.dateDiff = function (objDate2, interval) {
var d = this, i = {}, t = d.getTime(), t2 = objDate2.getTime();
i['y'] = objDate2.getFullYear() - d.getFullYear();
i['q'] = i['y'] * 4 + Math.floor(objDate2.getMonth() / 4) - Math.floor(d.getMonth() / 4);
i['m'] = i['y'] * 12 + objDate2.getMonth() - d.getMonth();
i['ms'] = objDate2.getTime() - d.getTime();
i['w'] = Math.floor((t2 + 345600000) / (604800000)) - Math.floor((t + 345600000) / (604800000));
i['d'] = Math.floor(t2 / 86400000) - Math.floor(t / 86400000);
i['h'] = Math.floor(t2 / 3600000) - Math.floor(t / 3600000);
i['n'] = Math.floor(t2 / 60000) - Math.floor(t / 60000);
i['s'] = Math.floor(t2 / 1000) - Math.floor(t / 1000);
return i[interval];
};
-----------------------------------------------------------------------------------------------------------------------------------------------------------
2017.12.15 更新
4、获取url中的参数
/* 获取URL查询参数 */
window.getQueryString = function (param) {
var sQuery = window.location.search;
if (sQuery && sQuery.length > 0) {
var arr = sQuery.substr(1).split('&');
for (var i in arr) {
var pair = arr[i].split('=');
if (param && param.toLowerCase() === pair[0].toLowerCase() && pair[1]) {
return pair[1];
}
}
}
return '';
};
5、封装jquery的ajax方法
var getDataSource = function (api, dataparam, successFunc, errorFunc) {
$.ajax({
url: api,
type: 'GET',
data: dataParam
dataType: 'json',
contentType: 'application/json',
success: function (response) {
successFunc.call(this, response);
},
error: function () {
errorFunc.call(this);
}
});
}
6、js断言函数
var assert = function(value, desc){
var li = document.createElement('li');
li.className = value ? 'pass' : 'fail';
li.appendChild(document.createTextNode(desc));
document.getElementById('result').appendChild(li);
}
分配一个pass或者fail的样式,该样式却绝育断言参数的值value,并将新元素附加到body内的列表元素上。
----------------------------------------------------------------
2018.3.27更新
8、js替换换行符和回车符
//替换所有的回车换行
var TransferString = function(content) {
var string = content;
try{
string=string.replace(/\r\n/g,"<br>")
string=string.replace(/\n/g,"<br>");
}catch(e) {
alert(e.message);
}
return string;
}
2018.10.16更新
9、去除对象多余属性(使用loadsh)
let model = {
edb: null,
cloud: null
};
param = _.pick(dataParam, _.keys(model));
model中是用户想要保存下来的属性,dataParam是要处理的对象。
10、判断变量类型