-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
48 lines (45 loc) · 1023 Bytes
/
util.js
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 延迟
const delay = function (ms) {
return new Promise((resolve) => {
setTimeout(() => {resolve()}, ms);
});
}
// 获取1到up的随机整数,含up
const getRandom = function (up) {
return Math.ceil(Math.random()*up);
}
// 等级class转换为等级
const levelClassToLevel = function(node) {
for(let i = 0; i < 30; i++) {
if(node.hasClass('vip_icon_m'+i) || node.hasClass('vip_icon_s'+i)){
return i;
}
}
return 0;
}
/**
* 生成1 - up 之间含1含up的n个不重复随机数
* @param {number} 上限
* @param {number} 个数
*/
const getRandomN = function(up, n) {
let set = new Set();
if(n >= up) {
for(let i = 1; i <= up; i++) {
set.add(i);
}
}else {
while(set.size < n) {
set.add(getRandom(up));
}
}
let res = [...set];
res.sort((v1, v2) => { return v1 - v2; });
return res;
}
module.exports = {
delay,
getRandom,
levelClassToLevel,
getRandomN
}