1、定时器
setTimeout
:循环一次。
setInterval
:循环屡次。
clearTimeout
:清除一次性定时器。
clearInterval
:清除屡次定时器。
function boom(){
console.log('boom');
}
setTimeout(boom, 1000);
function go(){
console.log('起床了')
}
setInterval(go, 3000);
var timer = setInterval(function(){
console.log('2');
},1000);
console.log(timer);
clearInterval(timer);
复制代码
2、回调函数
function test(d){
var a = 1;
var b = a +1;
var c = b;
d(a,b,c);
}
test(function(a,b,c){
console.log(a,b,c)
})
复制代码
3、同步代码和异步代码
- 同步代码:前面的代码没有执行完,会阻塞后面的代码执行。
- 异步代码:等知足条件的时间去执行。
- 异步加载: 1)定时器,动画帧 2)事件绑定的内容 3)Ajax采用的也是异步操做 4)回调函数
console.time();
setTimeout(function(){
console.timeEnd();
},0);
for(var i = 0 ;i < 10000;i++){
for(var j = 0;j< 10000;j++){
Math.random();
}
}
复制代码