setTimeout和setInterval
1.setTimeout 1)第一个参数 // 第一个参数不仅可以是function, 还可以是一段可执行代码的字符串格式 setTimeout('console.log(1)', 1000); // 一秒后打印1 // 如果上面的代码没有字符串包含,则会立即打印,定时器不起效果 setTimeout(function timer() { console.log(1); },1000); // 效果和上面的字符串功能相同 2) 第二个参数 setTimeout(fn) 等同于 setTimeout(fn, 0); // 第二个参数没有的时候默认是0,但其实是等于4ms 3)第三个及之后的参数 setTimeout(function timer(a,b) { console.log(a,b); // 1,2 }, 1000, 1,2); // 当有第三个及之后的参数时,作为回调函数的参数传入 4) 返回值 // 返回一个数字,表示定时器编号,数字以此加1 const a = setTimeout(fn, 1000); // a =4 const b = setTimeout(fn, 1000); // b = 5 const c = setTimeout(fn, 1000); // c = 6 5) 关于this // this可以理解成调用该函数的对象 // 1) 直接写入回调函数