1、时间格式化
Date.prototype.format=function(pattern){//日期格式化
var returnValue=pattern;
var format={
"y+":this.getFullYear(),
"M+":this.getMonth()+1,
"d+":this.getDate(),
"H+":this.getHours(),
"m+":this.getMinutes(),
"s+":this.getSeconds(),
"S":this.getMilliseconds(),
"h+":(this.getHours()%12),
"a":(this.getHours()/12)<=1?"AM":"PM"
};
for(var key in format){
var regExp=new RegExp("("+key+")");
if(regExp.test(returnValue)){
var zero="";
for(var i=0;i<RegExp.$1.length;i++){
zero+="0"
}
var replacement=RegExp.$1.length==1?format[key]:(zero+format[key]).substring(((""+format[key]).length));
returnValue=returnValue.replace(RegExp.$1,replacement)
}
}
return returnValue
};
用法:
new Date().format("yyyy-MM-dd HH:00:00")
ajax封装:
function ajax(obj){//ajax请求
$.ajax({
url:obj.url,//请求地址
type:obj.type||"POST",//请求方式
async:(function(){//obj.async||true,//是否异步
if(obj.async==false){
return false;
}else{
return true;
}
})(),
cache:obj.cache||true,//是否从浏览器缓存中加载请求信息
data:obj.data||"",//请求参数
dataType:obj.dataType||"json",//返回的数据类型
timeout:30000,
beforeSend : function(jqXHR, setting) {//数据查询过程中
if(!obj.noneOpen){//
$("#loading",parent.document).show();
}
},
complete : function(XMLHttpRequest, textStatus) {//数据查询超时
if(textStatus=="timeout"){
$("#loading",parent.document).hide();
msg({
layer:obj.layer,
title:"查询数据超时"
});
}
},
success : function(data) {//数据查询成功
if(obj.func){
obj.func(data);
}
$("#loading",parent.document).hide();
},
error : function(error) {//数据查询失败
if(obj.error){
obj.error(error);
}
$("#loading",parent.document).hide();
}
});
}
ajax用法:
ajax({
url:path+"/electric/cost/data",
data:elem,
layer:layer,
func:function(data){
console.log(data);
}
});