// console.log(a); // undefined // var a = 10; // 上面两行代码相当于 // console.log(a); // undefined // var a; // a = 10; // js有预解析过程:把声明提升带最前面 // var a; // console.log(a); // undefined // a = 10; // 普通函数也可以预解析 // fun(); // function fun() { // console.log('fun'); // }
return
// 封装获取样式函数 // ele:元素 attr:样式名(字符串) function getStyle (ele, attr) { // if (window.getComputedStyle) { // 标准浏览器 // return window.getComputedStyle(ele)[attr]; // } else { // 低版本IE // return ele.currentStyle[attr]; // } return window.getComputedStyle ? window.getComputedStyle(ele)[attr] : ele.currentStyle[attr]; }
// setTimeout(函数, 延迟时间):延迟固定时间执行一次函数,只会执行一次。 // setTimeout(function () { // console.log('timeout'); // }, 2000); function fun () { console.log('fun'); } // fun(); setTimeout(fun, 2000);
// setInterval(函数, 间隔时间):每间隔固定时间执行一次函数。 // setInterval(function () { // console.log(Math.random()); // }, 1000); function random () { console.log(Math.random()); } // random(); setInterval(random, 1000);
var t1 = setTimeout(function () { console.log('timeout'); }, 1000); clearTimeout(t1); var t2 = setInterval(function () { console.log('interval'); }, 1000); clearInterval(t2);