JS常用方法
整数随机数函数
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}
监听长按事件PC
$(".xx").touched(function () {
timeout = setTimeout(function () {
console.log('长按事件触发')
}, 1000);
});
$(".xx").mouseup(function () {
clearTimeout(timeout);
});
$(".xx").mouseout(function () {
clearTimeout(timeout);
});
监听长按事件移动端
$(".wx span").on({
touchstart: function() {
timeout = setTimeout(function () {
$('.gowx').click()
}, 1000);
},
touchmove: function() {
clearTimeout(timeout);
},
touchend: function() {
clearTimeout(timeout);
}
})
复制到剪切板
function copyTXT() {
const copyStr = wxArr[index]
const oInput = document.createElement('input');
oInput.value = copyStr;
document.body.appendChild(oInput);
oInput.select();
const copyResult = document.execCommand('copy')
document.body.removeChild(oInput)
}
数字保留一位小数
function fomatFloat(value, n) {
var f = Math.round(value * Math.pow(10, n)) / Math.pow(10, n);
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
s += '.';
}
for (var i = s.length - s.indexOf('.'); i <= n; i++) {
s += "0";
}
return s;
},
声明数组
const array = Array(5).fill('');
(5) ["", "", "", "", ""]