随笔分类 - js
js经验积累
摘要:replaceImgSrc(content) console.log(replaceImgSrc(content)) function replaceImgSrc(str) { var newStr = str.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/gi
阅读全文
摘要:1.instanceof 运算符 它可以用来检测是否为数组 var arr = []; var obj = {}; console.log(arr instanceof Array); // true console.log(obj instanceof Array); // false 2.Arr
阅读全文
摘要:1.核心算法:输入的时间减去现在的时间就是剩余的时间,即倒计时,但是不能拿时分秒 相减,比如05分减去25分,结果是负数。 2.用时间戳来做。用户输入时间总的毫秒数减去现在时间的总的毫秒数,得到的就是剩余 时间的毫秒数。 3.把剩余时间总的毫秒数转换为天、时、分、秒(时间戳转换为时分秒) 转换公式如
阅读全文
摘要:1.通过 valueOf()、getTime() var date = new Date(); console.log(date.valueOf()); // 就是 我们现在时间 距离1970.1.1 总的毫秒数 console.log(date.getTime()); 2.简单的写法 (最常用的写
阅读全文
摘要:Math.round(1.5) // 2 Math.round(-1.5) // -1 往大的取, 1 和 2 比较2大,所以Math.round(1.5) 的结果为 2; -2 和 -1 比较-1大,所以Math.round(-1.5) 的结果为 -1;
阅读全文
摘要:function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1) + min) } function shuffle(arr) { let _arr = arr.slice() // 创建一个源数组
阅读全文
摘要:function addClass(el, className) { if (hasClass(el, className)) { return } let newClass = el.className.split(' ') newClass.push(className) el.classNam
阅读全文
摘要:var mode = { 0: 'sequence', 1: 'loop', 2: 'random' } var m = 1 function changeMode() { var newMode = (m + 1) % 3 console.log(mode[newMode]) m++ } chan
阅读全文
摘要:function _pad(num, n = 2) { let len = num.toString().length while (len < n) { num = '0' + num len++ } return num }let seconds = 8_pad(seconds) // '08'
阅读全文
摘要:const express = require('express') const axios = require('axios') const bodyParser = require('body-parser') const app = express() const apiRoutes = ex
阅读全文
摘要:使用setTimeout将touchmove等事件延时,可以提高性能。 在搜索功能中也可以使用函数节流 export function debounce(func, delay = 200) { let timer return function (...args) { if (timer) { c
阅读全文

浙公网安备 33010602011771号