js生成随机名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var filename = "原名称";
var pos = filename.lastIndexOf('.')
var suffix = ''
if (pos != -1) {
suffix = filename.substring(pos)
}
var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789';
var maxPos = chars.length;
var timestamp = Date.parse(new Date())+'';
timestamp = timestamp.substring(timestamp.length-8);
var pwd = '';
for (i = 0; i < 10; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
//随机名称
name = pwd+timestamp+suffix;

ajax请求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
var loadIndex=null;
$.ajax({
type:"POST",
url:'',
data:form,
processData:false,
contentType : false,
dataType:'json',
beforeSend: function () {
loadIndex = layer.load(1, {
shade: [0.1, '#fff']
});
},
complete: function () {
layer.close(loadIndex);
},
success:function (res) {
if(res.status==200){
console.log(res);
}else{
layer.msg(res.msg,{icon:2});
}
},
error:function (xhr, type, errorThrown) {
layer.msg('上传错误,代码'+xhr.status,{icon:2});
}
});

js数的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 丢弃小数部分,保留整数部分
parseInt(5/2)  // 2

// 向上取整,有小数就整数部分加1
Math.ceil(5/2)  // 3

// 向下取整,丢弃小数部分
Math.floor(5/2)  // 2

// 四舍五入
Math.round(5/2)  // 3

// 取余
6%4  // 2