js中常用的方法
1、n-m之间的随机数
function numRandom(n, m) { if (n > m) { return parseInt(m + Math.random() * (n - m + 1)); } else { return parseInt(n + Math.random() * (m - n + 1)); } }
2.生成唯一的id
export function uuid() { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; }
3.时间过滤器
1. filters = (data) =>{ var time = new Date(data); var timeStr = time.getFullYear()+"-"+ (time.getMonth()+1).toString().padStart(2,"0")+"-"+ time.getDate().toString().padStart(2,"0")+ " "+ time.getHours().toString().padStart(2,"0")+":"+ time.getMinutes().toString().padStart(2,"0")+":"+ time.getSeconds().toString().padStart(2,"0"); return timeStr; }
2. function formatDate(value) {
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();
// return year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
return year + '-' + (String(month).length > 1 ? month : '0' + month) + '-' +
(String(day).length > 1 ? day : '0' + day) + ' ' + (String(hour).length > 1 ? hour : '0' + hour) + ':' + (String(minute).length > 1 ? minute : '0' + minute)
+ ':' + (String(second).length > 1 ? second : '0' + second)
}
//如果记得时间戳是毫秒级的就需要*1000 不然就错了记得转换成整型
var d = new Date(1553547600000); //Tue Mar 26 2019 05:00:00 GMT+0800 (中国标准时间)
console.log(formatDate(d)) //2019-03-26 05:00:00
4.返回顶部通用方法
function backTop(btnId) { var btn = document.getElementById(btnId); var d = document.documentElement; var b = document.body; window.onscroll = set; btn.style.display = "none"; btn.onclick = function () { btn.style.display = "none"; window.onscroll = null; this.timer = setInterval(function () { d.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1); b.scrollTop -= Math.ceil((d.scrollTop + b.scrollTop) * 0.1); if (d.scrollTop + b.scrollTop == 0) clearInterval(btn.timer, (window.onscroll = set)); }, 10); }; function set() { btn.style.display = d.scrollTop + b.scrollTop > 100 ? "block" : "none"; } } backTop("goTop");

浙公网安备 33010602011771号